How do I obtain the windows time zone string from Javascript or jQuery? - Stack Overflow

I have a dropdown list of timezones based of of .NET method System.TimeZoneInfo.GetSystemTimeZones prov

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?

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?

Share Improve this question edited Jan 24, 2020 at 15:39 Natalka asked Jan 24, 2020 at 14:01 NatalkaNatalka 4381 gold badge3 silver badges17 bronze badges 5
  • 1 See here – yW0K5o Commented Jan 24, 2020 at 14:03
  • 1 Does this answer your question? Get name of time zone – Heretic Monkey Commented Jan 24, 2020 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 Commented Jan 24, 2020 at 14:08
  • 1 Does this answer your question? How can I get the timezone name in JavaScript? – Ashkan Pourghasem Commented Jan 24, 2020 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 Commented Jan 24, 2020 at 15:25
Add a ment  | 

4 Answers 4

Reset to default 6

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

new Date().toString().split('(')[1].split(')')[0]

How about this?

Intl.DateTimeFormat().resolvedOptions().timeZone

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

Official Moment documentation: https://momentjs./

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

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744206069a4563108.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信