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

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.

Leave a Comment

How to? Ask a Question