使用 moment (timezone) . js 从用户浏览器获取时区

使用 Moment.js 和 moment-timezone 获取客户端时区并将其转换为其他时区的最佳方法是什么。JS

我想找出什么是客户的时区,然后把他的日期和时间转换成其他时区。

有人有这方面的经验吗?

189841 次浏览
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.

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.

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()
}

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

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

if the user's timezone is all you wanted then

const localtz = moment.tz.guess() // returns user's timezone

Additionally if you wanted to use it then the best way to convert a timestamp to user's timezone is

const time = moment.tz(response.timestamp)
const localtz = moment.tz.guess() // user's timezone
const date = time.clone().tz(localtz) // convert time to user's timezone

here localtz is the user's timezone and using it we can convert the timestamp to user's local time

First, you can find out the clients time zone using the following

let zoneVal = moment().tz(Intl.DateTimeFormat().resolvedOptions().timeZone).format('Z')

it will return you the GMT zone format for example +5:30 (colombo/srilanka & Delhi/India) or +6:00(Dhaka Bangladesh) depending on the region you are in.

secondly, if you want to find out the time of a particular time zone , then do the following

moment.tz("Asia/Dhaka").format()

which will return you the time zone value in ISO format of Dhaka.

Using moment timezone you can get easily your local date-time

moment().utcOffset(0, true).format()