首页 > 解决方案 > 不考虑用户时区

问题描述

我在约会方面遇到了麻烦。

注意:我进入欧洲/罗马时区。

我有以下日期

....
new Date("2019-04-03T00:00:00+02:00")

 it returns:  Wed Apr 03 2019 00:00:00 GMT+0200 (Ora legale dell’Europa centrale)
....

如果我在我的 PC 上更改时区 (GMT -4),它会返回,例如,

....
new Date("2019-04-03T00:00:00+02:00")
 it returns:  Tue Apr 02 2019 18:00:00 GMT-0400 (GMT-04:00)
....

我想要做的是在不考虑用户时区的情况下将日期(2019-04-03T00:00:00+02:00)到用户日历(即使它在加拿大我想在用户日历欧洲/罗马上显示时区。

你有什么建议吗?

谢谢

标签: javascriptangularjsdatetime

解决方案


通常,您只想将日期用作 UTC,并在显示它们时将它们转换为您想要的任何 TZ。 js中的UTC

时刻对这些东西很好,

const moment = require("moment-timezone")
const now = moment.utc()
const nowButItsInYourTimezone = now.tz("Europe/Rome")
console.log(now.format()) // utc date
console.log(nowButItsInYourTimezone.format()) // gmt + 2, of the same date

推荐阅读