【问题标题】:Converting timezone specific time into UTC using MomentJS使用 MomentJS 将时区特定时间转换为 UTC
【发布时间】:2016-07-13 19:18:44
【问题描述】:

Casablanca 时间转换为 UTC 时,MomentJS 使用了错误的时间:

moment.tz.add("Africa/Casablanca|LMT WET WEST CET|u.k 0 -10 -10|0121212121212121213121212121212121212121212121212121212121212121212121212121212121212121212121212121|-2gMnt.E 130Lt.E rb0 Dd0 dVb0 b6p0 TX0 EoB0 LL0 gnd0 rz0 43d0 AL0 1Nd0 XX0 1Cp0 pz0 dEp0 4mn0 SyN0 AL0 1Nd0 wn0 1FB0 Db0 1zd0 Lz0 1Nf0 wM0 co0 go0 1o00 s00 dA0 vc0 11A0 A00 e00 y00 11A0 uM0 e00 Dc0 11A0 s00 e00 IM0 WM0 mo0 gM0 LA0 WM0 jA0 e00 Rc0 11A0 e00 e00 U00 11A0 8o0 e00 11A0 11A0 5A0 e00 17c0 1fA0 1a00 1a00 1fA0 17c0 1io0 14o0 1lc0 11A0 1o00 11A0 1qM0 WM0 1qM0 WM0 1qM0 11A0 1o00 11A0 1o00 11A0 1qM0 WM0 1qM0 WM0 1qM0 WM0 1qM0 11A0 1lc0 14o0 1fA0|32e5");


alert(moment.tz({ year: 2016, month: 6, date: 13, hour: 20 }, "Africa/Casablanca")._d.toUTCString());

这显示20:13,但它应该显示19:13(UTC),不是吗?

JSFIDDLE

【问题讨论】:

    标签: javascript datetime momentjs utc


    【解决方案1】:

    AFAIK,使用像 _d 这样的内部结构可能会导致意外结果。尝试使用公共 API:

    moment.tz(...).toDate().toUTCString()
    

    【讨论】:

    • 这是不推荐的过程。除非您将日期传递给第三方 API,否则不要使用日期对象。查看对应的github问题:github.com/moment/moment/issues/3301#issuecomment-232474475
    • @MaggiePint 很公平,尽管我认为访问日期对象是有原因的,因此建议使用 toDate() :-)
    猜你喜欢
    • 2020-03-21
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    • 2020-06-16
    • 1970-01-01
    • 2014-08-12
    • 2021-07-13
    相关资源
    最近更新 更多