How to create time in a specific time zone with moment.js

前端 未结 4 1719
無奈伤痛
無奈伤痛 2020-12-06 04:09

I have this backend that sends me a pre formatted time in a set time zone, but without any information for the said time zone. The strings are like: \"2013-08-26 16:55:00\".

4条回答
  •  无人及你
    2020-12-06 04:42

    In most cases, you can simply do this:

    moment.tz("2013-08-26 16:55:00", "America/Los_Angeles")
    

    If you require input other than ISO8601, then specify the format string as the second parameter, and the time zone as the third:

    moment.tz("8/26/2013 4:55 pm", "M/D/YYYY h:mm a", "America/Los_Angeles")
    

    And if you need to use moment's "strict parsing" mode, then that goes in the third parameter, and the time zone moves to the fourth position:

    moment.tz("8/26/2013 4:55 pm", "M/D/YYYY h:mm a", true, "America/Los_Angeles")
    

提交回复
热议问题