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

前端 未结 5 607
伪装坚强ぢ
伪装坚强ぢ 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:09

    All current answers provide the offset differece at current time, not at a given date.

    moment(date).utcOffset() returns the time difference in minutes between browser time and UTC at the date passed as argument (or today, if no date passed).

    Here's a function to parse correct offset at the picked date:

    function getUtcOffset(date) {
      return moment(date)
        .subtract(
          moment(date).utcOffset(), 
          'minutes')
        .utc()
    }
    
    0 讨论(0)
  • 2020-12-02 20:15
    var timedifference = new Date().getTimezoneOffset();
    

    This returns the difference from the clients timezone from UTC time. You can then play around with it as you like.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-02 20:21

    When using moment.js, use:

    var tz = moment.tz.guess();
    

    It will return an IANA time zone identifier, such as America/Los_Angeles for the US Pacific time zone.

    It is documented here.

    Internally, it first tries to get the time zone from the browser using the following call:

    Intl.DateTimeFormat().resolvedOptions().timeZone
    

    If you are targeting only modern browsers that support this function, and you don't need Moment-Timezone for anything else, then you can just call that directly.

    If Moment-Timezone doesn't get a valid result from that function, or if that function doesn't exist, then it will "guess" the time zone by testing several different dates and times against the Date object to see how it behaves. The guess is usually a good enough approximation, but not guaranteed to exactly match the time zone setting of the computer.

    0 讨论(0)
  • 2020-12-02 20:28

    You can also get your wanted time using the following JS code:

    new Date(`${post.data.created_at} GMT+0200`)
    

    In this example, my received dates were in GMT+0200 timezone. Instead of it can be every single timezone. And the returned data will be the date in your timezone. Hope this will help anyone to save time

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