1

I need to get the TimeZone Name using Jquery using the code below.

When I select UTC+5.30(chennai,kolkatta,mumbai,newdelhi) Time zone in system settings, I am getting "India Standard Time" in edge and chrome browser.

But when I change to (UTC+5.30) Sri Jayawardenepura in settings, Chrome is showing "India Standard Time", Edge is showing "Srilanka Standard Time" which is the correct one. Is Anyone able to find why the difference and give me a solution?

I have used

(new Date).toString().split('(')[1].slice(0, -1);

var timeZone = (new Date).toString().split('(')[1].slice(0, -1);
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Subrak
  • 139
  • 11
  • Dupe of this? https://stackoverflow.com/questions/36475804/how-to-get-sri-lanka-time-abbreviation-from-date-function-in-javascript – mplungjan Aug 16 '19 at 12:21

2 Answers2

2

The approach you are using extracts the value in parenthesis from the output of Date.prototype.toString. That value is defined by tzName under TimeZoneString in section 20.3.4.41.3 of the current ECMAScript spec (10.0 at time of me writing this).

It says (emphasis mine):

Let tzName be an implementation-defined string that is either the empty string or the string-concatenation of the code unit 0x0020 (SPACE), the code unit 0x0028 (LEFT PARENTHESIS), an implementation-dependent timezone name, and the code unit 0x0029 (RIGHT PARENTHESIS).

Thus, you should not expect them to be any particular value on any particular browser, nor should you expect them to match.

That said, I can explain the behavior you are noticing.

  • Edge takes its tzName value from the localized display name associated with the time zone you have chosen. These are in the registry at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\Sri Lanka Standard Time, in the Std or Dlt field, whichever is in effect for the date you are working with.

  • Chrome takes its tzName value from the Unicode CLDR internationalization data (via ICU):

    • It first uses the windowsZones.xml file to map the Sri Lanka Standard Time Windows time zone ID to the Asia/Colombo IANA time zone ID.

      <!-- (UTC+05:30) Sri Jayawardenepura -->
      <mapZone other="Sri Lanka Standard Time" territory="001" type="Asia/Colombo"/>
      <mapZone other="Sri Lanka Standard Time" territory="LK" type="Asia/Colombo"/>
      
    • It then uses the metaZones.xml file to assign Asia/Colombo to the India CLDR MetaZone.

      <timezone type="Asia/Colombo">
          <usesMetazone to="1996-05-24 18:30" mzone="India"/>
          <usesMetazone to="2006-04-14 18:30" from="1996-05-24 18:30" mzone="Lanka"/>
          <usesMetazone from="2006-04-14 18:30" mzone="India"/>
      </timezone>
      
    • It then uses the active language file, such as en.xml to determine the display name to use for the India MetaZone.

      <metazone type="India">
          <long>
              <standard>India Standard Time</standard>
          </long>
      </metazone>
      

      This becomes the tzName returned in the parenthesis in Date.prototype.toString.

Note that while Edge and Chrome use different sources for the Date.prototype.toString output, Edge does also use CLDR for it's implementation of the ECMAScript Internationalization API. This is visible with the following:

new Date().toLocaleString(undefined, {timeZoneName: 'long'})

With your time zone set for Sri Lanka, both Edge and Chrome will return:

"8/16/2019, 10:41:31 PM India Standard Time"

A few other points:

  • Note that the display value will change also by language setting of the operating system, as well as by the time zone part that is in effect for the date provided. For example, in the US Pacific time zone, in English, you will see either "Pacific Standard Time" or "Pacific Daylight Time". You will see different values in French, Japanese, etc. For this reason, it is intended to be used for display to end-users only. It should not be parsed apart and treated as an identifier.

  • To get an actual IANA tzdb identifier, such as Asia/Colombo or Asia/Kolkata or America/Los_Angeles, see https://stackoverflow.com/a/22625076/634824

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
0

Timezones are confusing.

What you need is the time zone name.

Timezone names are usually of the form 'Asia/city_name'

Along with the date and utc offset, that's what you would need to do time conversions (and always do those in UTC before converting to local time)

The time zone offset name you get from chrome or edge may change along the year anyway if the place you're looking at observes daylight saving, so it is not the right thing to use.

For time/timezone lookup, use momentjs timezone

https://momentjs.com/timezone/

MrE
  • 19,584
  • 12
  • 87
  • 105