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

前端 未结 4 1711
無奈伤痛
無奈伤痛 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:41

    install moment-timezone

    > npm install moment-timezone

    Or see https://momentjs.com/timezone/docs/

    .tz(string, string)

    moment.tz("2020-01-02 13:33:37", "Iran/Tehran")

    0 讨论(0)
  • 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")
    
    0 讨论(0)
  • 2020-12-06 04:43

    If you want to calculate everything in a specific timezone you want to set the default time zone using

    A) moment.tz.setDefault("America/Los_Angeles");

    For my use case (in a node.js project) I just set it right after requiring the moment modules like so:

    let moment = require('moment');
    require('moment-timezone');
    moment.tz.setDefault("America/Los_Angeles");
    

    All calls to moment() thereafter will create the time in the "America/Los_Angeles" setting, which is NOT the same as using:

    B) moment.tz("2017-03-04 00:00", "America/Los_Angeles")

    OR

    C) moment("2017-03-04 00:00").tz("America/Los_Angeles")

    both of which would create the moment object in UTC time (unless you already changed the default), and then convert it to be the Los Angeles timezone.

    Running B or C above in the browser console yields:

    _d: Fri Mar 03 2017 16:00:00 GMT-0800 (PST)
    _i: "2017-3-4 00:00"
    

    Notice _d shows March 3 4:00pm; this is because the moment object is created with March 4 12:00am in UTC time, then converted to Pacific timezone, which is 8 hours behind/the previous day.

    source: http://momentjs.com/timezone/docs/#/using-timezones/default-timezone/

    0 讨论(0)
  • 2020-12-06 04:46

    I had a similar issue for which i had to use New York based time so i had to consider for daylight savings. I tried using the above few answers but wasn't able to get it working. Then I solved my issue like the below code

    import moment from 'moment-timezone'
    
    const time = timestamp
    const offset = moment.tz.zone('America/New_York')?.parse(time)
    const date = moment(time).add(offset, 'minutes').toISOString()
    

    this gives you an ISO string which you can use as below

    moment(date).local().format('ffffdd, MMM DD YYYY, hh:mm A')

    or in whatever format you would like to display it

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