如何将日期格式化为 (dd/mm/yyyy hh:mm:ss)

如何将日期格式化为 (dd/mm/yyyy hh:mm:ss)

问题描述:

如何将下面的日期转换成这个模板 (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);

这是我的解决方案.如果要创建高级格式,可以阅读有关 object Intl 的更多信息https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl

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')))