简体   繁体   中英

How to get the timezone string of the browser in Javascript?

I want to display on my webpapge the full-name of the user's Timezone . For example:

Eastern Standard Time (North America)
Pacific Daylight Time (North America)
West Africa Time

How can I do this in Javascript? I have seen solutions that render something like this: America/New_York . But that's not really what I'm looking for.

一个班轮:

new window.Intl.DateTimeFormat().resolvedOptions().timeZone

You appear to be asking for a human-readable description of the user's current time zone, in the user's current language, at the current point in time.

In most modern browsers and other environments that fully support the ECMAScript Internationalization API this is best extracted as follows:

// Get a specific point in time (here, the current date/time):
const d = new Date();

// Get a DateTimeFormat object for the user's current culture (via undefined)
// Ask specifically for the long-form of the time zone name in the options
const dtf = Intl.DateTimeFormat(undefined, {timeZoneName: 'long'});

// Format the date to parts, and pull out the value of the time zone name
const result = dtf.formatToParts(d).find((part) => part.type == 'timeZoneName').value;

For example, in my Chrome 79 browser on Windows in winter, it returns "Pacific Standard Time". The same code in summer will return "Pacific Daylight Time". If you want to reflect the description of the time zone that is in effect at a particular time of the year, then create the Date object at that particular time.

In older environments that don't support the Intl API, you can try extracting part of the time zone name from the Date.toString output like this:

var s = new Date().toString().match(/\((.*)\)/).pop();

This only works because the output of the toString function on the Date object is left up to the implementation to define, and many implementations happen to supply the time zone in parenthesis, like this:

Sun Feb 21 2016 22:11:00 GMT-0800 (Pacific Standard Time)

If you were looking for a generic name, such as simply "Pacific Time" - sorry, that's not available.

One way to do this is by using momentJs library.

http://momentjs.com/

First install/import momentjs in your page. with this you can get time in UTC. Now from UTC you can convert to client's local time or to any other time zone using momentJS timezone.

http://momentjs.com/timezone/


UPDATE 2

Sorry the above library does not detect the timezone for now.

I used this and it seems to work for me.

<body>
<div id="testArea"></div>
<script type="text/javascript">
        var counter = 1;
        var interval = setInterval(function () {
            document.getElementById('testArea').innerHTML = counter + ": " + jstz.determine().name() + "<br/>";
            counter++;
        }, 2000);
</script>

</body>

the repository is here to download and try once you download the repository extract it and go in utilities folder and open index.html.

: https://bitbucket.org/pellepim/jstimezonedetect/downloads

the page where sample demo is shown is here:

http://pellepim.bitbucket.org/jstz/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM