1

I have a dropdown list of timezones based of of .NET method System.TimeZoneInfo.GetSystemTimeZones provided from my MVC controller. What I would like to do is capture the user's timezone (client side) and default the dropdown list to their timezone.

On both Chrome and Firefox when I type in new Date() to the console I can get a string like Fri Jan 24 2020 08:50:02 GMT-0500 (Eastern Standard Time)

Other than parsing between the parentheses, is there a way to get the timezone string Eastern Standard Time?

Natalka
  • 258
  • 1
  • 14
  • 1
    See [here](https://stackoverflow.com/questions/18246547/get-name-of-time-zone) – yW0K5o Jan 24 '20 at 14:03
  • 1
    Does this answer your question? [Get name of time zone](https://stackoverflow.com/questions/18246547/get-name-of-time-zone) – Heretic Monkey Jan 24 '20 at 14:07
  • 1
    @yW0K5o You should be able to flag the question as a duplicate if you find another question that answers the current one. – Heretic Monkey Jan 24 '20 at 14:08
  • 1
    Does this answer your question? [How can I get the timezone name in JavaScript?](https://stackoverflow.com/questions/9772955/how-can-i-get-the-timezone-name-in-javascript) – Ashkan Pourghasem Jan 24 '20 at 14:08
  • The problem with Intl.DateTimeFormat().resolvedOptions().timeZone & moment.tz.guess(); is that I get "America/New_York" rather than "Eastern Standard Time" (And "America/New_York" isn't an option on the dropdown that's generated from GetSystemTimeZones) – Natalka Jan 24 '20 at 15:25

4 Answers4

6

I used this to get the gmt text. It is rought, but might work

new Date().toString().split('(')[1].split(')')[0]
pliniocf
  • 352
  • 2
  • 8
3

How about this?

Intl.DateTimeFormat().resolvedOptions().timeZone
1

You can get the current timezone offset in minutes from the dates getTimezoneOffset function. Then you can divide the number by 60 to get the actual offset in hours. Note that the offset is the additatively inverted number of the "GMT+0100" string.

const offset = new Date().getTimezoneOffset() / 60;
console.log('Offset: ', offset);
console.log('UTC:    ', new Date().toUTCString());
console.log('GMT:    ', new Date().toString());

See the docs:

The getTimezoneOffset() method returns the time zone difference, in minutes, from current locale (host system settings) to UTC.

Felix Lemke
  • 6,189
  • 3
  • 40
  • 67
  • That would work great for the offset, but I'm actually looking for the string, i.e. "Eastern Standard Time". The dropdown menu I have has multiple options for each GMT time. (For example "Eastern Time Zone" and "Cuba Standard Time" have the same GMT time but are different options). – Natalka Jan 24 '20 at 15:32
  • Can't you use the offset as the dropdown "values" and the full string as the "label"? – Felix Lemke Jan 24 '20 at 15:39
  • "Eastern Standard Time" is the actual value I need, my label is actually "(UTC-05:00) Eastern Time (US & Canada)". The label could be changed if that would help but "Eastern Standard Time" is what I need to ultimately send to the server. – Natalka Jan 24 '20 at 15:43
1

I can suggest to use Moment, which is a third party library for handling everything from time to dates. I really really recommend this.

Official Moment documentation: https://momentjs.com/

In your question you can get the time zone really easy using moment like:

var jun = moment("2014-06-01T12:00:00Z");
var dec = moment("2014-12-01T12:00:00Z");

jun.tz('America/Los_Angeles').format('z');  // PDT
dec.tz('America/Los_Angeles').format('z');  // PST

jun.tz('America/New_York').format('z');     // EDT
dec.tz('America/New_York').format('z');     // EST

// This gets you your current timezone
moment().tz(Intl.DateTimeFormat().resolvedOptions().timeZone).format('z')

// Other examples
jun.tz('Asia/Tokyo').format('ha z');           // 9pm JST
dec.tz('Asia/Tokyo').format('ha z');           // 9pm JST

jun.tz('Australia/Sydney').format('ha z');     // 10pm EST
dec.tz('Australia/Sydney').format('ha z');     // 11pm EST
Ling Vu
  • 4,740
  • 5
  • 24
  • 45