Detect the ID of the current user timezone using moment.js

前端 未结 4 779
隐瞒了意图╮
隐瞒了意图╮ 2020-12-05 18:40

What I\'m looking for is a way to detect the browser\'s timezone ID (as defined in the Olson tables) but I don\'t care for the exact ID, I just need the ID of a timezone tha

相关标签:
4条回答
  • There's a javascript tool that does just that :

    https://github.com/entraigas/jstz

    It seems to deal with timezones ambiguity also.

    Combined with momentJS timezone, you can get the timezone and show formatted date :

    var tzObj = jstz.determine();
    var timezoneCode = tzObj.name();
    console.log(moment.tz(timeZoneCode).format());
    
    0 讨论(0)
  • 2020-12-05 19:08

    I made a small script to do that detection. It starts by registering the ids of the available timezones, then, on a call to the matches function tests all timezone ids for the current time and the times 4 and 8 months later (to filter out the timezones with different daylight rules) and five years before.

    Here it is :

    <script src="moment-with-langs.min.js"></script>
    <script src="moment-timezone.min.js"></script>
    <script src="moment-timezone-data.js"></script>
    <script>
    var tzdetect = {
        names: moment.tz.names(),
        matches: function(base){
            var results = [], now = Date.now(), makekey = function(id){
                return [0, 4, 8, -5*12, 4-5*12, 8-5*12, 4-2*12, 8-2*12].map(function(months){
                    var m = moment(now + months*30*24*60*60*1000);
                    if (id) m.tz(id);
                    return m.format("DDHHmm");
                }).join(' ');
            }, lockey = makekey(base);
            tzdetect.names.forEach(function(id){
                if (makekey(id)===lockey) results.push(id);
            });
            return results;
        }
    };
    </script>
    

    If you just want one timezone id, simply use

    var tzid = tzdetect.matches()[0];
    

    Demonstration

    GitHub Repository : https://github.com/Canop/tzdetect.js

    Update : The code here shown isn't compatible with the most recent versions of moment.js. Please refer to the GitHub repository for a maintained (free to use) code.

    2017 Update: There's now an API in moment.js to guess the timezone. That's probably the best solution right now.

    0 讨论(0)
  • 2020-12-05 19:19

    moment now has the guess() API as described here

    0 讨论(0)
  • 2020-12-05 19:21

    If you want to use the standard JavaScript API, you can use Intl.DateTimeFormat.resolvedOptions where there is browser support:

    Intl.DateTimeFormat().resolvedOptions().timeZone; // "America/Los_Angeles"
    

    resolvedOptions is currently (Jan 2016) available in all browsers except Safari on iOS and desktop: http://caniuse.com/#feat=internationalization

    However, the timeZone property is currently only available on Chrome.

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