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

JavascriptMomentjsAngular Moment

Javascript Problem Overview


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 timezone and later convert his date and time into some other timezone.

Does anybody has experience with this?

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

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.

Solution 3 - Javascript

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

Solution 4 - Javascript

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

Solution 5 - Javascript

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

Solution 6 - Javascript

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

Solution 7 - Javascript

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionnemo_87View Question on Stackoverflow
Solution 1 - JavascriptMatt Johnson-PintView Answer on Stackoverflow
Solution 2 - JavascriptjogoeView Answer on Stackoverflow
Solution 3 - JavascripttaoView Answer on Stackoverflow
Solution 4 - JavascriptPhilippeView Answer on Stackoverflow
Solution 5 - JavascriptDavidView Answer on Stackoverflow
Solution 6 - JavascriptNishithView Answer on Stackoverflow
Solution 7 - JavascriptHasibul Alam RahiView Answer on Stackoverflow