我已将我的OS时区设置为UTC-11 (萨摩亚)。当我这么做时:
const date = new Date("1990-01-01"); // in my real scenario, the user will set a random date using a datepicker
const randomLocale = Math.random() < 0.5 ? "en" : "es";
console.log(date.toLocaleDateString(randomLocale, {
year: "numeric",
month: "long",
day: "numeric",
}));不出所料,我有一天的假期:
“2018年12月31日”
为了解决这个问题,我决定使用moment.js,如下所示:
console.log(moment(date).utc().format())其结果是:
"2019-01-01T00:00:00+00:00“
好的!但是..。如何才能获得与使用toLocaleDateString()相同格式的输出字符串?
发布于 2022-07-22 14:00:56
两者都有
console.log(moment(date).utc().format("ll"))
和
console.log(moment(date).utc().format("LL"))
工作正确!
https://stackoverflow.com/questions/73081103
复制相似问题