在javascript中,有没有办法将日期时间转换为以下格式:
// 11/3/18, 12:00 AM
Date().toString() gives me:
Sat Nov 03 2018 00:00:00 GMT+0000 (UTC)
谢谢。
最佳答案
这是格式化日期的替代方法,函数 Date.prototype.toLocaleDateString
允许您根据选项/标志设置日期格式。
一些 js 引擎以不同的方式管理格式过程(因此,这是依赖于实现的),因此要小心。此外,您需要检查浏览器的兼容性。
let today = new Date();
var options = { year: 'numeric', month: 'numeric', day: 'numeric', hour12: true, hour: 'numeric', minute: 'numeric' };
console.log(today.toLocaleDateString('en-US', options));
关于javascript - 如何在 JavaScript 中获取特定的日期时间格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53135815/