0

It appears that between v8 and v10 of Node, and around a similar time in Chrome (currently reproduced in version 69.0.3497.100), the Date.toString method stopped returning the time zone portion of the output as an acronym and started returning it as whole words.

The Date.toString example at MDN indicates that

new Date('August 19, 1975 23:15:30').toString()

Should return an acronym in initials, like

Tue Aug 19 1975 23:15:30 GMT+0100 (BST)

Which I've confirmed works in Node 8. However, when I run the same code in Node v10, I receive this:

Tue Aug 19 1975 23:15:30 GMT+0100 (British Summer Time)

Without using any string code or manually making the initials from the words, is there a way to extract the initials of the current time zone?

Note that any solution should work in Node AND Chrome.

jackweirdy
  • 5,632
  • 4
  • 24
  • 33
  • The change was reported in earlier Chrome versions, like in here: https://stackoverflow.com/questions/50814643/chrome-67-date-vs-chrome-66-date. So I don't think you should rely on `Date.prototype.toString` for consistent result. – blaz Oct 19 '18 at 10:38
  • Thanks for finding that link, useful context. The decision to rely on it was made long before it changed unfortunately! – jackweirdy Oct 19 '18 at 10:48

1 Answers1

0

The solution should be to set the timeZoneName property of the options object to either "short" or "long" (MDN link) — but that’s just giving me GMT+1 for short:

> d = new Date
> d.toLocaleString('en-US',{timeZone:'America/Los_Angeles',timeZoneName:'long'}) 
'10/19/2018, 3:31:04 AM Pacific Daylight Time'
> d.toLocaleString('en-US',{timeZone:'America/Los_Angeles',timeZoneName:'short'})
'10/19/2018, 3:31:04 AM PDT'
> d.toLocaleString('en-US',{timeZone:'Europe/London',timeZoneName:'long'})
'10/19/2018, 11:31:04 AM British Summer Time'
> d.toLocaleString('en-US',{timeZone:'Europe/London',timeZoneName:'short'})
'10/19/2018, 11:31:04 AM GMT+1'

Firefox is also giving me GMT+1 for short and in addition GMT+01:00 for long.

If you reliably need zone names, look into Moment — see their section on abbreviated time zone names.

Josh Lee
  • 171,072
  • 38
  • 269
  • 275