Initialize a Moment with the timezone offset that I created it with

前端 未结 1 629
情书的邮戳
情书的邮戳 2020-12-09 21:09

I\'m using moment and moment-timezone in javascript, and this part of it is one of the most unintuitive API\'s I\'ve ever seen.

I would expect that:

         


        
相关标签:
1条回答
  • 2020-12-09 21:51

    Use parseZone to keep the offset as it was passed in.

    moment.parseZone("2015-12-14T04:00:00Z")
    

    As to the "why?" part of your question:

    • moment(...) is local mode. Ambiguous input (without offset) is assumed to be local time. Unambiguous input (with offset) is adjusted to local time.
    • moment.utc(...) is utc mode. Ambiguous input is assumed to be UTC. Unambiguous input is adjusted to UTC.
    • moment.parseZone() keep the input zone passed in. If the input is ambiguous, it is the same as local mode.
    • moment.tz(...) with the moment-timezone plugin can parse input in a specific time zone.

    Keep in mind that moment has to contend with a wide variety of inputs.

    Also keep in mind that a time zone and a time zone offset are two different things. An offset of -08:00 doesn't necessarily mean you are in the US Pacific time zone.

    0 讨论(0)
提交回复
热议问题