如何将日期格式化为 (dd/mm/yyyy hh:mm:ss) [英] How to format the date to (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')))
这篇关于如何将日期格式化为 (dd/mm/yyyy hh:mm:ss)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文