要标准化新的 Date().toLocaleString()
,可以使用 Intl.DateTimeFormat
对象来代替。
以下是一个示例代码:
const date = new Date();
const options = {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
timeZone: 'UTC',
};
const formattedDate = new Intl.DateTimeFormat('en-US', options).format(date);
console.log(formattedDate);
在上面的示例中,我们创建了一个 options
对象,其中包含了需要的日期和时间格式。然后,我们使用 Intl.DateTimeFormat
对象来格式化日期。最后,将格式化后的日期打印到控制台。
这种方法可以确保返回的日期字符串具有标准化的格式,而不依赖于浏览器或操作系统的本地化设置。