Get timezone from users browser using moment(timezone).js

前端 未结 5 622
伪装坚强ぢ
伪装坚强ぢ 2020-12-02 19:40

What is the best way to get client\'s timezone and convert it to some other timezone when using moment.js and moment-timezone.js

I want to find out what is clients t

5条回答
  •  爱一瞬间的悲伤
    2020-12-02 20:15

    Using Moment library, see their website -> https://momentjs.com/timezone/docs/#/using-timezones/converting-to-zone/

    i notice they also user their own library in their website, so you can have a try using the browser console before installing it

    moment().tz(String);
    
    The moment#tz mutator will change the time zone and update the offset.
    
    moment("2013-11-18").tz("America/Toronto").format('Z'); // -05:00
    moment("2013-11-18").tz("Europe/Berlin").format('Z');   // +01:00
    
    This information is used consistently in other operations, like calculating the start of the day.
    
    var m = moment.tz("2013-11-18 11:55", "America/Toronto");
    m.format();                     // 2013-11-18T11:55:00-05:00
    m.startOf("day").format();      // 2013-11-18T00:00:00-05:00
    m.tz("Europe/Berlin").format(); // 2013-11-18T06:00:00+01:00
    m.startOf("day").format();      // 2013-11-18T00:00:00+01:00
    
    Without an argument, moment#tz returns:
    
        the time zone name assigned to the moment instance or
        undefined if a time zone has not been set.
    
    var m = moment.tz("2013-11-18 11:55", "America/Toronto");
    m.tz();  // America/Toronto
    var m = moment.tz("2013-11-18 11:55");
    m.tz() === undefined;  // true
    

提交回复
热议问题