Get name of time zone
Asked Answered
D

14

38

Can I get the name of the time zone of the client by using jQuery?

Example: Pacific S.A. Standard Time, Greenwich Standard Time, like this: https://en.wikipedia.org/wiki/List_of_time_zones_by_country

Timezonelist

Dato answered 15/8, 2013 at 4:54 Comment(9)
The output of new Date() is Wed Aug 14 2013 22:02:13 GMT-0700 (Pacific Daylight Time), can you use that?Cheesecake
There's no native JS/jQuery function that does this. One way of doing this is to combine JS (AJAX) with server side code, such as PHP. Look at the top answer in this thread. It combines JS and PHP and does what you want.Folksy
@ItayItai You don't think my answer satisfies the requirement? It's the same name from the list the OP gaveCheesecake
@Juan Mendes: You're right, but I was explaining the idea of using AJAX for this mission. He can be more flexible with the server side code (assuming he needs something more specific than your answer)Folksy
@Juan Mendes In Safari on Mac , it not Wed Aug 14 2013 22:02:13 GMT-0700 (Pacific Daylight Time), it like Wed Aug 14 2013 22:02:13 GMT-0700 (PCI). Not my anwser i need, I need get Timezone Name like Pacific S.A. Standard Time, Greenwich Standard Time . thank you for responseBallistics
@NathanNguyễn There is enough information through the comments and answer for you to solve your problem, you should post what you come up with.Cheesecake
possible duplicate of JavaScript timezone information how to get America/Los_Angeles (or equivalent)Spine
@NathanNguyễn see this plugin it will give you timezone name and many more kevalbhatt.github.io/WorldMapGeneratorZoosporangium
No need for jQuery. The question stackoverflow.com/questions/9772955/… is essentially the same as this and has several useful answers.Suggestion
S
53

Can I get the name of the time zone of the client by using jQuery?

No. jQuery has only one date/time function, which is $.now().

If you meant JavaScript, the answer is still no. You can only obtain a time zone offset from date.getTimezoneOffset(). You can't get a time zone - at least not in all browsers. Refer to the timezone tag wiki's section titled: "Time Zone != Offset"

You can guess at the time zone, by using the jsTimeZoneDetect library, but it is just a guess. It may or may not be accurate.

You can also now use moment.js with the moment-timezone add on. It now supports time zone guessing with moment.tz.guess().

If you can guarantee your users are running in an environment that fully supports the ECMAScript Internationalization API, you can get the user's time zone like this:

Intl.DateTimeFormat().resolvedOptions().timeZone

You can review the compatibility table, under DateTimeFormat - resolvedOptions().timezone defaults to the host environment to determine which environments this will work in.

Honestly, the best thing to do is to just give your user a screen somewhere that they can select their timezone. You might use a drop-down list, or you might use a map-based timezone picker - like this one. You can use jsTimeZoneDetect as a default value, but your user should be able to change it.

Also, all of these are going to give you an IANA time zone identifier, such as America/Los_Angeles. But the examples you gave appear to be Windows time zone ids (for use with TimeZoneInfo in .net). You should read the timezone tag wiki, and then also this question: How to translate between Windows and IANA time zones?

Spine answered 15/8, 2013 at 12:3 Comment(4)
@KevalBhatt - Nice! I'll keep that in mind for questions about time zone pickers. It's a good alternative for the one I linked to above. Thanks!Spine
(new Date).toString().split('(')[1].slice(0, -1)Borgeson
@Borgeson - that's not reliable. The results could be anything. The implementations vary considerably across browsers, and there is no spec.Spine
With the compatibility on Intl.DateTimeFormat().resolvedOptions(), it would seem this is the best option now-a-days. Thanks!Rumney
C
17

The output of new Date() is Wed Aug 14 2013 22:02:13 GMT-0700 (Pacific Daylight Time), can you use that? If you don't need the official name, you can just use what's in between the parentheses

function getTimeZone() {
    return /\((.*)\)/.exec(new Date().toString())[1];
}

getTimeZone(); // Pacific Daylight Time 
Cheesecake answered 15/8, 2013 at 5:8 Comment(4)
In Safari on Mac , it not Wed Aug 14 2013 22:02:13 GMT-0700 (Pacific Daylight Time), it like Wed Aug 14 2013 22:02:13 GMT-0700 (PCI). Not my anwser i need, I need get Timezone Name like Pacific S.A. Standard Time, Greenwich Standard Time . thank you for responseBallistics
@Juan - That response is going to be highly browser, language, and OS specific.Spine
@MattJohnson Which may be a good thing if you're used to seeing your time zone in your own OS, browser and language :) and you don't have a need for official time zone names.Cheesecake
This is helpful if you want "Pacific Daylight Time" instead of "America/Los_Angeles". But @MattJohnson-Pint is right, this isn't very robust. Instead, here's a browser/language/OS-inspecific solution: https://mcmap.net/q/21167/-get-name-of-time-zoneMaximalist
R
14

As an alternative below is the way to get timezone with pure javascript:

(new Date).toString().split('(')[1].slice(0, -1)
Rapine answered 3/2, 2016 at 14:37 Comment(0)
W
8

Get the current timezone standard name:

function getTimezoneName() {
    var timeSummer = new Date(Date.UTC(2005, 6, 30, 0, 0, 0, 0));
    var summerOffset = -1 * timeSummer.getTimezoneOffset();
    var timeWinter = new Date(Date.UTC(2005, 12, 30, 0, 0, 0, 0));
    var winterOffset = -1 * timeWinter.getTimezoneOffset();
    var timeZoneHiddenField;

    if (-720 == summerOffset && -720 == winterOffset) { timeZoneHiddenField = 'Dateline Standard Time'; }
    else if (-660 == summerOffset && -660 == winterOffset) { timeZoneHiddenField = 'UTC-11'; }
    else if (-660 == summerOffset && -660 == winterOffset) { timeZoneHiddenField = 'Samoa Standard Time'; }
    else if (-660 == summerOffset && -600 == winterOffset) { timeZoneHiddenField = 'Hawaiian Standard Time'; }
    else if (-570 == summerOffset && -570 == winterOffset) { timeZoneHiddenField.value = 'Pacific/Marquesas'; }
    //                else if (-540 == summerOffset && -600 == winterOffset) { timeZoneHiddenField.value = 'America/Adak'; }
    //                else if (-540 == summerOffset && -540 == winterOffset) { timeZoneHiddenField.value = 'Pacific/Gambier'; }
    else if (-480 == summerOffset && -540 == winterOffset) { timeZoneHiddenField = 'Alaskan Standard Time'; }
    //                else if (-480 == summerOffset && -480 == winterOffset) { timeZoneHiddenField = 'Pacific/Pitcairn'; }
    else if (-420 == summerOffset && -480 == winterOffset) { timeZoneHiddenField = 'Pacific Standard Time'; }
    else if (-420 == summerOffset && -420 == winterOffset) { timeZoneHiddenField = 'US Mountain Standard Time'; }
    else if (-360 == summerOffset && -420 == winterOffset) { timeZoneHiddenField = 'Mountain Standard Time'; }
    else if (-360 == summerOffset && -360 == winterOffset) { timeZoneHiddenField = 'Central America Standard Time'; }
    //                else if (-360 == summerOffset && -300 == winterOffset) { timeZoneHiddenField = 'Pacific/Easter'; }
    else if (-300 == summerOffset && -360 == winterOffset) { timeZoneHiddenField = 'Central Standard Time'; }
    else if (-300 == summerOffset && -300 == winterOffset) { timeZoneHiddenField = 'SA Pacific Standard Time'; }
    else if (-240 == summerOffset && -300 == winterOffset) { timeZoneHiddenField = 'Eastern Standard Time'; }
    else if (-270 == summerOffset && -270 == winterOffset) { timeZoneHiddenField = 'Venezuela Standard Time'; }
    else if (-240 == summerOffset && -240 == winterOffset) { timeZoneHiddenField = 'SA Western Standard Time'; }
    else if (-240 == summerOffset && -180 == winterOffset) { timeZoneHiddenField = 'Central Brazilian Standard Time'; }
    else if (-180 == summerOffset && -240 == winterOffset) { timeZoneHiddenField = 'Atlantic Standard Time'; }
    else if (-180 == summerOffset && -180 == winterOffset) { timeZoneHiddenField = 'Montevideo Standard Time'; }
    else if (-180 == summerOffset && -120 == winterOffset) { timeZoneHiddenField = 'E. South America Standard Time'; }
    else if (-150 == summerOffset && -210 == winterOffset) { timeZoneHiddenField = 'Mid-Atlantic Standard Time'; }
    else if (-120 == summerOffset && -180 == winterOffset) { timeZoneHiddenField = 'America/Godthab'; }
    else if (-120 == summerOffset && -120 == winterOffset) { timeZoneHiddenField = 'SA Eastern Standard Time'; }
    else if (-60 == summerOffset && -60 == winterOffset) { timeZoneHiddenField = 'Cape Verde Standard Time'; }
    else if (0 == summerOffset && -60 == winterOffset) { timeZoneHiddenField = 'Azores Daylight Time'; }
    else if (0 == summerOffset && 0 == winterOffset) { timeZoneHiddenField = 'Morocco Standard Time'; }
    else if (60 == summerOffset && 0 == winterOffset) { timeZoneHiddenField = 'GMT Standard Time'; }
    else if (60 == summerOffset && 60 == winterOffset) { timeZoneHiddenField = 'Africa/Algiers'; }
    else if (60 == summerOffset && 120 == winterOffset) { timeZoneHiddenField = 'Namibia Standard Time'; }
    else if (120 == summerOffset && 60 == winterOffset) { timeZoneHiddenField = 'Central European Standard Time'; }
    else if (120 == summerOffset && 120 == winterOffset) { timeZoneHiddenField = 'South Africa Standard Time'; }
    else if (180 == summerOffset && 120 == winterOffset) { timeZoneHiddenField = 'GTB Standard Time'; }
    else if (180 == summerOffset && 180 == winterOffset) { timeZoneHiddenField = 'E. Africa Standard Time'; }
    else if (240 == summerOffset && 180 == winterOffset) { timeZoneHiddenField = 'Russian Standard Time'; }
    else if (240 == summerOffset && 240 == winterOffset) { timeZoneHiddenField = 'Arabian Standard Time'; }
    else if (270 == summerOffset && 210 == winterOffset) { timeZoneHiddenField = 'Iran Standard Time'; }
    else if (270 == summerOffset && 270 == winterOffset) { timeZoneHiddenField = 'Afghanistan Standard Time'; }
    else if (300 == summerOffset && 240 == winterOffset) { timeZoneHiddenField = 'Pakistan Standard Time'; }
    else if (300 == summerOffset && 300 == winterOffset) { timeZoneHiddenField = 'West Asia Standard Time'; }
    else if (330 == summerOffset && 330 == winterOffset) { timeZoneHiddenField = 'India Standard Time'; }
    else if (345 == summerOffset && 345 == winterOffset) { timeZoneHiddenField = 'Nepal Standard Time'; }
    else if (360 == summerOffset && 300 == winterOffset) { timeZoneHiddenField = 'N. Central Asia Standard Time'; }
    else if (360 == summerOffset && 360 == winterOffset) { timeZoneHiddenField = 'Central Asia Standard Time'; }
    else if (390 == summerOffset && 390 == winterOffset) { timeZoneHiddenField = 'Myanmar Standard Time'; }
    else if (420 == summerOffset && 360 == winterOffset) { timeZoneHiddenField = 'North Asia Standard Time'; }
    else if (420 == summerOffset && 420 == winterOffset) { timeZoneHiddenField = 'SE Asia Standard Time'; }
    else if (480 == summerOffset && 420 == winterOffset) { timeZoneHiddenField = 'North Asia East Standard Time'; }
    else if (480 == summerOffset && 480 == winterOffset) { timeZoneHiddenField = 'China Standard Time'; }
    else if (540 == summerOffset && 480 == winterOffset) { timeZoneHiddenField = 'Yakutsk Standard Time'; }
    else if (540 == summerOffset && 540 == winterOffset) { timeZoneHiddenField = 'Tokyo Standard Time'; }
    else if (570 == summerOffset && 570 == winterOffset) { timeZoneHiddenField = 'Cen. Australia Standard Time'; }
    else if (570 == summerOffset && 630 == winterOffset) { timeZoneHiddenField = 'Australia/Adelaide'; }
    else if (600 == summerOffset && 540 == winterOffset) { timeZoneHiddenField = 'Asia/Yakutsk'; }
    else if (600 == summerOffset && 600 == winterOffset) { timeZoneHiddenField = 'E. Australia Standard Time'; }
    else if (600 == summerOffset && 660 == winterOffset) { timeZoneHiddenField = 'AUS Eastern Standard Time'; }
    else if (630 == summerOffset && 660 == winterOffset) { timeZoneHiddenField = 'Australia/Lord_Howe'; }
    else if (660 == summerOffset && 600 == winterOffset) { timeZoneHiddenField = 'Tasmania Standard Time'; }
    else if (660 == summerOffset && 660 == winterOffset) { timeZoneHiddenField = 'West Pacific Standard Time'; }
    else if (690 == summerOffset && 690 == winterOffset) { timeZoneHiddenField = 'Central Pacific Standard Time'; }
    else if (720 == summerOffset && 660 == winterOffset) { timeZoneHiddenField = 'Magadan Standard Time'; }
    else if (720 == summerOffset && 720 == winterOffset) { timeZoneHiddenField = 'Fiji Standard Time'; }
    else if (720 == summerOffset && 780 == winterOffset) { timeZoneHiddenField = 'New Zealand Standard Time'; }
    else if (765 == summerOffset && 825 == winterOffset) { timeZoneHiddenField = 'Pacific/Chatham'; }
    else if (780 == summerOffset && 780 == winterOffset) { timeZoneHiddenField = 'Tonga Standard Time'; }
    else if (840 == summerOffset && 840 == winterOffset) { timeZoneHiddenField = 'Pacific/Kiritimati'; }
    else { timeZoneHiddenField = 'US/Pacific'; }
    return timeZoneHiddenField;
}
Wiltshire answered 12/2, 2014 at 7:32 Comment(4)
Why do wee need two parameters (summerOffset, winterOffset)?Belter
@PhucNguyen Some timezones observe daylight savings time, others don't.Wangle
Its like Bollywood movie!Axenic
Please note that Brazil no longer has daylight savings so this is outdated... timeanddate.com/time/change/brazilCheesecake
O
4

Found this script I am using with moment.js:

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

This script allows to get the name of the timezone of the browser, like in this exemple:

> var timezone = jstz.determine();
> timezone.name(); 
"America/New_York"

It is not in jQuery, however...

Ontina answered 21/1, 2015 at 15:40 Comment(2)
Welcome to StackOverflow! Posting just a link, without an associated code sample, is not terribly helpful. Please try to avoid posting just links without further information.Amanita
Unfortunately this library is not up to date, so some timezones can be detected incorrectly.Erl
W
3

In pure javascript using regular expression,

var d = new Date();
var s = d.toString();
var zoneName = s.match(".*(\\((.*)\\))")[2];
Warranty answered 10/2, 2017 at 11:18 Comment(0)
T
1

Can't think of any way to do it using Jquery. You can get the remote user country name using PHP and then compare the output to:

1) an array of countries that apply light saving time (not many).

2) current date.

To get the users country:

function visitor_country() {
    $ip = $_SERVER["REMOTE_ADDR"];
    if(filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    if(filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP))
            $ip = $_SERVER['HTTP_CLIENT_IP'];
    $result = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip))
            ->geoplugin_countryName;
    return $result <> NULL ? $result : "Unknown";
}
echo visitor_country(); // Output Country name [Ex: United States]
?>

This world Time Zones map link can be used to create the array: http://www.worldtimezone.com/daylight.html

Hope this idea works for you!

Thickening answered 30/4, 2014 at 1:7 Comment(0)
B
1

console.log(Intl.DateTimeFormat().resolvedOptions().timeZone);
Intl.DateTimeFormat().resolvedOptions().timeZone
Botsford answered 7/4, 2021 at 7:12 Comment(0)
M
1

The Modern Approach

Matt Johnson-Pint's answer gives output such as "America/Los_Angeles" in IANA time zone database (tzdata) format:

Intl.DateTimeFormat().resolvedOptions().timeZone

// -> "America/Los_Angeles"

console.log(Intl.DateTimeFormat().resolvedOptions().timeZone);

If you instead prefer the exact name of the timezone, like "Pacific Standard Time", you can use the following expression:

Intl.DateTimeFormat(undefined, { timeZoneName: "long" })
    .formatToParts(new Date())
    .find((part) => part.type === "timeZoneName").value

// -> "Pacific Standard Time"

console.log(Intl.DateTimeFormat(undefined, { timeZoneName: "long" }).formatToParts(new Date()).find((part) => part.type === "timeZoneName").value);

There are alternatives to { timeZoneName: "long" } if you prefer another format from the list:

  • "long" -> "Pacific Standard Time"

  • "short" -> "PST"

  • "shortOffset" -> "GMT-8"

  • "longOffset" -> "GMT-08:00"

  • "shortGeneric" -> "PT"

  • "longGeneric" -> "Pacific Time"

    ("Generic" means it won't swap between the two separately named time zones, "Pacific Standard Time"/"PST" and "Pacific Daylight Time"/"PDT", during parts of the year.)

This should work more robustly than Juan Mendes's answer because that approach works by delicately string matching the output of (new Date()).toString(). But all bets are off for other people using a different language, browser, or OS. The above expression should be more robust and give a translated name specific to the language of the user, like "Nordamerikanische Westküstenzeit" for German language users instead of "Pacific Time".

This is supported in all modern browsers, even Safari. It does not work in Internet Explorer. More details in the browser compatibility chart.

Maximalist answered 16/12, 2021 at 9:48 Comment(0)
A
0

These Links might help you out timeZone using Jquery

or following code might help u out

var offset = (new Date()).getTimezoneOffset();
alert(offset);

checkout JSFIddle implementation or for further explanation please visit Solution by Scott

For Only Time Zone

function get_time_zone_offset( ) {
    var current_date = new Date();
  alert(current_date);   
  //return -current_date.getTimezoneOffset() / 60;

}

The alert popup will display Timezone as well, which you can re-factor as per your requirement, check it out in JS Fiddle

Abiogenesis answered 15/8, 2013 at 5:1 Comment(2)
Thanks for response, but i dont want get offset number, I want to get Timezone Name like 'Pacific S.A. Standard Time, Greenwich Standard Time'.Ballistics
@NathanNguyễn you can format the JS Fiddle code to return TimeZone Name as per your requirement.Abiogenesis
M
0

using moment.js + moment-timezones.js

moment.tz.guess();

https://github.com/moment/moment-timezone/issues/138

Melgar answered 6/11, 2016 at 23:8 Comment(0)
E
0

This site has some nice examples

https://www.geeksforgeeks.org/javascript-date-now/

// Use of Date.now() function 
var d = Date(Date.now()); 

// Converting the number of millisecond in date string 
a = d.toString() 

// Printing the current date                     
document.write("The current date is: " + a)  
Entryway answered 21/1, 2020 at 14:27 Comment(0)
F
-1

I know this question is old, but here's a little solution I came up with:

(new Date).toLocaleString('en-US', {
    hour12: false,
    hour: '2-digit',
    timeZoneName: 'long'
}).replace(/^\d\d /, '');

I haven't tested this widely, but for my browser this gives me:

"Mountain Daylight Time"

Hope this helps somebody out there.

Folks answered 18/3, 2015 at 15:53 Comment(3)
@Gujamin: Sadly, you're right. It looks like Safari completely ignores the provided options object, which Chrome and other courteous browsers politely regard. Does anybody know a concise cross-platform way to obtain the timezone in long string format?Folks
I ended up just using user3119287's answer. Not ideal but it worked.Herr
Not working even in IE. It does not give the timezone nameHypersonic
R
-1

There is no direct way, but you can do this :

var dd = new Date(); var d = dd.toString();

var startOff= d.indexOf('(') + 1; var endOff= d.lastIndexOf(')');

var str = d.substring(startOff , endOff);

I have created a JS fiddle, where you see with alerts : JSFiddle

var dd = new Date();
var d = dd.toString();

alert(d);
var startOff= d.indexOf('(') + 1;
alert(startOff);
var endOff= d.lastIndexOf(')');
alert(startOff+ "  : " + endOff);

var str = d.substring(startOff , endOff);
alert(str);
Reagent answered 26/1, 2017 at 21:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.