How to convert a date time into format of dd/mm/yyyy in JavaScript?

5/5 - (1 vote)

If you need to convert a date-time like 2020-04-28 00:00:00 which comes for example from the backend, you can use the following JavaScript code:

<script type="text/javascript">
			function convertDate(inputDate) {
			  function change(s) { return (s < 10) ? '0' + s : s; }
			  var temp = new Date(inputDate);
			  return [change(temp.getDate()), change(temp.getMonth()+1), temp.getFullYear()].join('/');
			}
			
			var newDate = convertDate('2020-04-28 00:00:00');
			console.log(newDate); // 28/04/2020
</script>

A simple function that saves you a lot of hassle.
If you have other solutions, I invite you to post them in the comments.

Hello there!

I hope you find this post useful!

I'm Mihai, a programmer and online marketing specialist, very passionate about everything that means online marketing, focused on eCommerce.

If you have a collaboration proposal or need helps with your projects feel free to contact me. I will always be glad to help you!

subscribe youtube

Leave a Comment

WebPedia.net