Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format the date to (dd/mm/yyyy hh:mm:ss)

How can I convert the date below into this template (dd/mm/yyyy hh:mm:ss) ?

05/04/2021 14:52

I tried to do it that way, but I only get the time and not the date with time.

var data = new Date('05/04/2021 14:52');
var time = data.toLocaleTimeString('pt-PT', {hour12: false});
console.log(time);
like image 785
Isaac Avatar asked Oct 25 '25 02:10

Isaac


2 Answers

You can use below script

var data = new Date('05/04/2021 14:52');
console.log(data.toLocaleString('en-GB',{hour12: false}));

Output : "04/05/2021, 14:52:00"

like image 124
Ashish Sharma Avatar answered Oct 26 '25 16:10

Ashish Sharma


This is my solution. If you want to create a advanced format, you can read more about object Intl https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl

const formatDate = new Intl.DateTimeFormat("en" , {
  day: "2-digit",
  month: "2-digit",
  year: "numeric",
  hour: "2-digit",
  minute: "2-digit",
  second: "2-digit",
  hour12: false
});

console.log(formatDate.format(new Date('05/04/2021 14:52')))
like image 34
Dai Nguyen Avatar answered Oct 26 '25 16:10

Dai Nguyen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!