New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to find timezone abbreviation from Date.prototype.toString() #162

Closed
ghost opened this Issue Feb 6, 2012 · 30 comments

Comments

Projects
None yet
@ghost

ghost commented Feb 6, 2012

Hello I came across the moment.js site via a forum and clicked my way through it, when I came across the unit test - which failed 2 tests what I want to report here:

UserAgent:
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7 (Chrome 16.0.912.77 m)

Timezone:
UTC+1

Failed tests (# 28):
http://pastebin.com/bQsDuxdH

*Title edited by timrwood

@timrwood

This comment has been minimized.

Show comment
Hide comment
@timrwood

timrwood Feb 6, 2012

Member

Hmm, looks like the timezone abbreviation is failing. Sorry to do remote debugging, but what is the output of the following in chrome?

new Date().toString()

Also, what is the name of the timezone you are in?

Member

timrwood commented Feb 6, 2012

Hmm, looks like the timezone abbreviation is failing. Sorry to do remote debugging, but what is the output of the following in chrome?

new Date().toString()

Also, what is the name of the timezone you are in?

@Sertion

This comment has been minimized.

Show comment
Hide comment
@Sertion

Sertion Feb 11, 2012

I get the same error in the unit-tests and I'm in UTC+1 (previously known as GMT+1) as well.

new Date().toString() prints "Sat Feb 11 2012 02:15:10 GMT+0100" in Firefox 10, "Sat Feb 11 2012 02:18:29 GMT+0100 (W. Europe Standard Time)" in Chrome 16 and "Sat Feb 11 02:18:59 UTC+0100 2012" in Internet Explorer 9

Sertion commented Feb 11, 2012

I get the same error in the unit-tests and I'm in UTC+1 (previously known as GMT+1) as well.

new Date().toString() prints "Sat Feb 11 2012 02:15:10 GMT+0100" in Firefox 10, "Sat Feb 11 2012 02:18:29 GMT+0100 (W. Europe Standard Time)" in Chrome 16 and "Sat Feb 11 02:18:59 UTC+0100 2012" in Internet Explorer 9

@timrwood

This comment has been minimized.

Show comment
Hide comment
@timrwood

timrwood Feb 20, 2012

Member

Hmm, I was afraid of this. The problem is that Date.prototype.toString returns such different results. That is the only place to get the timezone name (PST, CST, EST, etc). If this method is not returning any timezone information (as is the case with FF10 and IE9, there is no way to get it.

Perhaps it's time to depreciate? Or is it acceptable to just note the issue in the docs?

Member

timrwood commented Feb 20, 2012

Hmm, I was afraid of this. The problem is that Date.prototype.toString returns such different results. That is the only place to get the timezone name (PST, CST, EST, etc). If this method is not returning any timezone information (as is the case with FF10 and IE9, there is no way to get it.

Perhaps it's time to depreciate? Or is it acceptable to just note the issue in the docs?

@bravecow

This comment has been minimized.

Show comment
Hide comment
@bravecow

bravecow Mar 3, 2012

4318 passed, 2 failed. 2715 milliseconds.

Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11

UTC +2 (Kyiv, Ukraine)

format timezone 2 passed,2 failed.
---> Something like "PST"
AssertionError: ---> Something like "PST"
at Object.ok (http://momentjs.com/js/tests.js?_=120203_183019:1526:39)
at http://momentjs.com/js/tests.js?_=120203_183019:2413:14
at Object.runTest (http://momentjs.com/js/tests.js?_=120203_183019:1702:9)
at http://momentjs.com/js/tests.js?_=120203_183019:1748:25
at http://momentjs.com/js/tests.js?_=120203_183019:1006:13
at http://momentjs.com/js/tests.js?_=120203_183019:616:13
at http://momentjs.com/js/tests.js?_=120203_183019:627:25
at http://momentjs.com/js/tests.js?_=120203_183019:1008:17
at http://momentjs.com/js/tests.js?_=120203_183019:1589:17
---> Something like "PST"
AssertionError: ---> Something like "PST"
at Object.ok (http://momentjs.com/js/tests.js?_=120203_183019:1526:39)
at http://momentjs.com/js/tests.js?_=120203_183019:2414:14
at Object.runTest (http://momentjs.com/js/tests.js?_=120203_183019:1702:9)
at http://momentjs.com/js/tests.js?_=120203_183019:1748:25
at http://momentjs.com/js/tests.js?_=120203_183019:1006:13
at http://momentjs.com/js/tests.js?_=120203_183019:616:13
at http://momentjs.com/js/tests.js?_=120203_183019:627:25
at http://momentjs.com/js/tests.js?_=120203_183019:1008:17
at http://momentjs.com/js/tests.js?_=120203_183019:1589:17
+02:00 ---> Something like "+07:30"
+0200 ---> Something like "+0700"

bravecow commented Mar 3, 2012

4318 passed, 2 failed. 2715 milliseconds.

Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11

UTC +2 (Kyiv, Ukraine)

format timezone 2 passed,2 failed.
---> Something like "PST"
AssertionError: ---> Something like "PST"
at Object.ok (http://momentjs.com/js/tests.js?_=120203_183019:1526:39)
at http://momentjs.com/js/tests.js?_=120203_183019:2413:14
at Object.runTest (http://momentjs.com/js/tests.js?_=120203_183019:1702:9)
at http://momentjs.com/js/tests.js?_=120203_183019:1748:25
at http://momentjs.com/js/tests.js?_=120203_183019:1006:13
at http://momentjs.com/js/tests.js?_=120203_183019:616:13
at http://momentjs.com/js/tests.js?_=120203_183019:627:25
at http://momentjs.com/js/tests.js?_=120203_183019:1008:17
at http://momentjs.com/js/tests.js?_=120203_183019:1589:17
---> Something like "PST"
AssertionError: ---> Something like "PST"
at Object.ok (http://momentjs.com/js/tests.js?_=120203_183019:1526:39)
at http://momentjs.com/js/tests.js?_=120203_183019:2414:14
at Object.runTest (http://momentjs.com/js/tests.js?_=120203_183019:1702:9)
at http://momentjs.com/js/tests.js?_=120203_183019:1748:25
at http://momentjs.com/js/tests.js?_=120203_183019:1006:13
at http://momentjs.com/js/tests.js?_=120203_183019:616:13
at http://momentjs.com/js/tests.js?_=120203_183019:627:25
at http://momentjs.com/js/tests.js?_=120203_183019:1008:17
at http://momentjs.com/js/tests.js?_=120203_183019:1589:17
+02:00 ---> Something like "+07:30"
+0200 ---> Something like "+0700"

@korg250

This comment has been minimized.

Show comment
Hide comment
@korg250

korg250 Mar 9, 2012

I also get the same failing test # 28. My timezone is GMT -3.

Agent:
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.66 Safari/535.11

Failed test: http://pastebin.com/dsWHMF3h

.:.

Agent:
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2
or
Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; Zune 4.7; .NET4.0E)

Failed test: http://pastebin.com/6t6JP9fu

korg250 commented Mar 9, 2012

I also get the same failing test # 28. My timezone is GMT -3.

Agent:
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.66 Safari/535.11

Failed test: http://pastebin.com/dsWHMF3h

.:.

Agent:
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2
or
Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; Zune 4.7; .NET4.0E)

Failed test: http://pastebin.com/6t6JP9fu

@timrwood

This comment has been minimized.

Show comment
Hide comment
@timrwood

timrwood Mar 13, 2012

Member

So looks like this is happening in UTC+1, UTC+2, and UTC-3.

Unfortunately, there is now way to fix this. It depends on hacking apart the Date().toString() output, and if there is no timezone information available in that string, there is no way to get that information.

I'm going to depreciate this feature and maybe make a plugin that has more reliable (although perhaps less accurate) results. It probably should be something like https://github.com/mde/timezone-js.

Member

timrwood commented Mar 13, 2012

So looks like this is happening in UTC+1, UTC+2, and UTC-3.

Unfortunately, there is now way to fix this. It depends on hacking apart the Date().toString() output, and if there is no timezone information available in that string, there is no way to get that information.

I'm going to depreciate this feature and maybe make a plugin that has more reliable (although perhaps less accurate) results. It probably should be something like https://github.com/mde/timezone-js.

@timrwood

This comment has been minimized.

Show comment
Hide comment
@timrwood

timrwood Apr 23, 2012

Member

The z zz tokens are being deprecated as of 1.6.0. I'm closing this issue as a result.

Member

timrwood commented Apr 23, 2012

The z zz tokens are being deprecated as of 1.6.0. I'm closing this issue as a result.

@timrwood timrwood closed this Apr 23, 2012

@DjebbZ

This comment has been minimized.

Show comment
Hide comment
@DjebbZ

DjebbZ Jan 11, 2013

What about node.js ? Are z zz reliable or not in this environment ?

DjebbZ commented Jan 11, 2013

What about node.js ? Are z zz reliable or not in this environment ?

@timrwood

This comment has been minimized.

Show comment
Hide comment
@timrwood

timrwood Jan 11, 2013

Member

They have been officially deprecated, so they will not work as of version 1.6.0.

For versions of code before that, I imagine the results would also be inconsistant, as Node uses V8 and some of the inconsistant results above are in Chrome, which also uses V8.

Member

timrwood commented Jan 11, 2013

They have been officially deprecated, so they will not work as of version 1.6.0.

For versions of code before that, I imagine the results would also be inconsistant, as Node uses V8 and some of the inconsistant results above are in Chrome, which also uses V8.

@DjebbZ

This comment has been minimized.

Show comment
Hide comment
@DjebbZ

DjebbZ Jan 11, 2013

Thanks. So for a string like Thu Jan 10 2013 22:54:11 GMT+0100 (CET), what I ended up doing is removing with a regexp the timezone string (here (CET) with the leading space) and leaving only the timeshift info (here +0100). Do you think it's enough for moment.js or do I risk inconsistencies ?

DjebbZ commented Jan 11, 2013

Thanks. So for a string like Thu Jan 10 2013 22:54:11 GMT+0100 (CET), what I ended up doing is removing with a regexp the timezone string (here (CET) with the leading space) and leaving only the timeshift info (here +0100). Do you think it's enough for moment.js or do I risk inconsistencies ?

@timrwood

This comment has been minimized.

Show comment
Hide comment
@timrwood

timrwood Jan 11, 2013

Member

Hmm, I'm not sure what you are trying to do, are you trying to get CET or +0100?

+0100 is supported via the ZZ token (note the capitalization) and works fine when parsing and formatting.

CET was deprecated as we couldn't reliably get it from Date.toString. However, as we use Date.getTimezoneOffset for +0100, the support is much more consistant.

Member

timrwood commented Jan 11, 2013

Hmm, I'm not sure what you are trying to do, are you trying to get CET or +0100?

+0100 is supported via the ZZ token (note the capitalization) and works fine when parsing and formatting.

CET was deprecated as we couldn't reliably get it from Date.toString. However, as we use Date.getTimezoneOffset for +0100, the support is much more consistant.

@DjebbZ

This comment has been minimized.

Show comment
Hide comment
@DjebbZ

DjebbZ Jan 11, 2013

I'm doing as you say, using ZZ for +0100 and removing CET from the string to parse. Does it make sense ?

DjebbZ commented Jan 11, 2013

I'm doing as you say, using ZZ for +0100 and removing CET from the string to parse. Does it make sense ?

@timrwood

This comment has been minimized.

Show comment
Hide comment
@timrwood

timrwood Jan 11, 2013

Member

My apologies, I'm still not understanding what the problem is here. Are you trying to parse Thu Jan 10 2013 22:54:11 GMT+0100 (CET)? You should be able to just do the following.

moment('Thu Jan 10 2013 22:54:11 GMT+0100 (CET)', 'ddd MMM DD YYYY HH:mm:ss [GMT]ZZ');
Member

timrwood commented Jan 11, 2013

My apologies, I'm still not understanding what the problem is here. Are you trying to parse Thu Jan 10 2013 22:54:11 GMT+0100 (CET)? You should be able to just do the following.

moment('Thu Jan 10 2013 22:54:11 GMT+0100 (CET)', 'ddd MMM DD YYYY HH:mm:ss [GMT]ZZ');
@DjebbZ

This comment has been minimized.

Show comment
Hide comment
@DjebbZ

DjebbZ Jan 11, 2013

This is exactly what I'm doing at the end. It looks like you're confirming that I'm doing the right thing. My apologies If I wasn't clear, thanks for the answers !

DjebbZ commented Jan 11, 2013

This is exactly what I'm doing at the end. It looks like you're confirming that I'm doing the right thing. My apologies If I wasn't clear, thanks for the answers !

@timrwood

This comment has been minimized.

Show comment
Hide comment
@timrwood

timrwood Jan 11, 2013

Member

No problem!

Member

timrwood commented Jan 11, 2013

No problem!

@timrwood

This comment has been minimized.

Show comment
Hide comment
@timrwood

timrwood Oct 10, 2014

Member

@hughanderson4, the issue was that browsers don't always return a timezone abbreviation from Date.prototype.toString. Because we couldn't reliably get the timezone abbreviation, it was deprecated.

Since then, moment-timezone has been created to add timezone support for moment.js. You may want to look into that to see if it fits your needs.

Member

timrwood commented Oct 10, 2014

@hughanderson4, the issue was that browsers don't always return a timezone abbreviation from Date.prototype.toString. Because we couldn't reliably get the timezone abbreviation, it was deprecated.

Since then, moment-timezone has been created to add timezone support for moment.js. You may want to look into that to see if it fits your needs.

@thinkjson

This comment has been minimized.

Show comment
Hide comment
@thinkjson

thinkjson Oct 28, 2014

How would one convert a moment.utc() to local time and display the time zone abbreviation?

thinkjson commented Oct 28, 2014

How would one convert a moment.utc() to local time and display the time zone abbreviation?

@machineghost

This comment has been minimized.

Show comment
Hide comment
@machineghost

machineghost Nov 4, 2014

Stupid question: why an entirely new library? Isn't the point of Moment to solve all of one's date and time-handling needs? While the data files add a non-trivial amount of space (and I can understand separating them), the new library itself is <2k, so space doesn't seem to be a reason for separating it.

It just seems kind of sad that someone who simply wants to generate a date string with a timezone in it has to download two separate libraries and a data file to do what Javascript should have built-in.

Also, how do I use this new amalgamation of libraries to generate a simple date string? Before I could have generated:

Sun, 06 Nov 1994 08:49:37 GMT

with:

moment().format('ddd, DD MMM YYYY HH:mm:ss z');

But now I'm confused as to how I'm supposed to to do that without the "z" formatting option. Any help would be appreciated.

machineghost commented Nov 4, 2014

Stupid question: why an entirely new library? Isn't the point of Moment to solve all of one's date and time-handling needs? While the data files add a non-trivial amount of space (and I can understand separating them), the new library itself is <2k, so space doesn't seem to be a reason for separating it.

It just seems kind of sad that someone who simply wants to generate a date string with a timezone in it has to download two separate libraries and a data file to do what Javascript should have built-in.

Also, how do I use this new amalgamation of libraries to generate a simple date string? Before I could have generated:

Sun, 06 Nov 1994 08:49:37 GMT

with:

moment().format('ddd, DD MMM YYYY HH:mm:ss z');

But now I'm confused as to how I'm supposed to to do that without the "z" formatting option. Any help would be appreciated.

@chris-martin

This comment has been minimized.

Show comment
Hide comment
@chris-martin

chris-martin Feb 8, 2015

So, asking just to make sure I understand: There is no replacement for the deprecated 'z' format?

chris-martin commented Feb 8, 2015

So, asking just to make sure I understand: There is no replacement for the deprecated 'z' format?

@mrhwick

This comment has been minimized.

Show comment
Hide comment
@mrhwick

mrhwick Feb 11, 2015

If I have a timestamp in the ISO-8601 format, such as:

2014-07-25T18:00:00-04:00

And I want to display this timestamp as the following:

6:00 PM EST

Does moment no longer support this formatting? All of the timezone data is included in my timestamp, and I only want to display it directly without transforming the timezone in any way.

mrhwick commented Feb 11, 2015

If I have a timestamp in the ISO-8601 format, such as:

2014-07-25T18:00:00-04:00

And I want to display this timestamp as the following:

6:00 PM EST

Does moment no longer support this formatting? All of the timezone data is included in my timestamp, and I only want to display it directly without transforming the timezone in any way.

@DeadMG

This comment has been minimized.

Show comment
Hide comment
@DeadMG

DeadMG Jun 23, 2015

That does not actually include the timezone. There can be several timezone names that correspond to the same UTC offset, especially when considering the possibilities of varying DST. You cannot infer from "UTC-4:00" that the timezone is EST.

DeadMG commented Jun 23, 2015

That does not actually include the timezone. There can be several timezone names that correspond to the same UTC offset, especially when considering the possibilities of varying DST. You cannot infer from "UTC-4:00" that the timezone is EST.

@mj1856

This comment has been minimized.

Show comment
Hide comment
@mj1856

mj1856 Aug 5, 2015

Member

For clarification, the z formatter still works when using moment-timezone with a specific zone - since the abbreviation can be pulled from the time zone data.

Member

mj1856 commented Aug 5, 2015

For clarification, the z formatter still works when using moment-timezone with a specific zone - since the abbreviation can be pulled from the time zone data.

@themakshter

This comment has been minimized.

Show comment
Hide comment
@themakshter

themakshter Oct 20, 2015

Just mentioning this now - if you use
new Date().toTimeString()
you get the same result across all browsers as far as I have tried.

This means that you could possibly use this string to extract the timezone, no?

themakshter commented Oct 20, 2015

Just mentioning this now - if you use
new Date().toTimeString()
you get the same result across all browsers as far as I have tried.

This means that you could possibly use this string to extract the timezone, no?

@Shobana16

This comment has been minimized.

Show comment
Hide comment
@Shobana16

Shobana16 Jan 7, 2016

Hi I see above that format('z') is deprecated. also in moment docs, i see it is been deprecated from 1.6.0. I am using 2.11.0 now, i can use the 'z' to display timezone (PST/PDT) based on offset.
Should i not to use this 'z' in my code now? can you please tell exact alternative to display PST instead of -/+8

Shobana16 commented Jan 7, 2016

Hi I see above that format('z') is deprecated. also in moment docs, i see it is been deprecated from 1.6.0. I am using 2.11.0 now, i can use the 'z' to display timezone (PST/PDT) based on offset.
Should i not to use this 'z' in my code now? can you please tell exact alternative to display PST instead of -/+8

@mj1856

This comment has been minimized.

Show comment
Hide comment
@mj1856

mj1856 Jan 13, 2016

Member

@themakshter - no, you do not get the same result across all browsers. OS, browser version, and language play into it heavily - and there is no consistency requirement in the spec.

Member

mj1856 commented Jan 13, 2016

@themakshter - no, you do not get the same result across all browsers. OS, browser version, and language play into it heavily - and there is no consistency requirement in the spec.

@mj1856

This comment has been minimized.

Show comment
Hide comment
@mj1856

mj1856 Jan 13, 2016

Member

@Shobana16 - If you just do moment().format('z'), it will always return "".

If you're using moment-timezone, and do something like moment.tz('America/Los_Angeles').format('z'), then you will get a valid response.

z is fine with moment-timezone. It does nothing on a plain moment.

Member

mj1856 commented Jan 13, 2016

@Shobana16 - If you just do moment().format('z'), it will always return "".

If you're using moment-timezone, and do something like moment.tz('America/Los_Angeles').format('z'), then you will get a valid response.

z is fine with moment-timezone. It does nothing on a plain moment.

@Shobana16

This comment has been minimized.

Show comment
Hide comment
@Shobana16

Shobana16 Jan 13, 2016

Thank you for the comments MJ. Yeah i am using 'z' with moment-timezone only. Ok cool will remain my code with 'z'. !

Shobana16 commented Jan 13, 2016

Thank you for the comments MJ. Yeah i am using 'z' with moment-timezone only. Ok cool will remain my code with 'z'. !

@mj1856

This comment has been minimized.

Show comment
Hide comment
@mj1856

mj1856 Feb 10, 2016

Member

Just a note to add, as of moment-timezone 0.5.0, you can guess at the local time zone, which opens the door for:

var abbreviation = moment.tz(moment.tz.guess()).format('z');

This is ok, but be aware that:

  1. It's just a guess. It might guess wrong.
  2. If it does guess wrong, there's still a possibility that the abbreviation could be correct, as many similar time zones will use the same abbreviations, such as how Europe/Paris and Europe/Berlin both use CET and CEST.
  3. However, there's no guarantees. If it guesses wrong, you might present the wrong abbreviation.

We might also consider adapting moment.js such that if moment-timezone is available that it could try to do this automatically to re-enable the z formatter. Though this is not happening currently.

Member

mj1856 commented Feb 10, 2016

Just a note to add, as of moment-timezone 0.5.0, you can guess at the local time zone, which opens the door for:

var abbreviation = moment.tz(moment.tz.guess()).format('z');

This is ok, but be aware that:

  1. It's just a guess. It might guess wrong.
  2. If it does guess wrong, there's still a possibility that the abbreviation could be correct, as many similar time zones will use the same abbreviations, such as how Europe/Paris and Europe/Berlin both use CET and CEST.
  3. However, there's no guarantees. If it guesses wrong, you might present the wrong abbreviation.

We might also consider adapting moment.js such that if moment-timezone is available that it could try to do this automatically to re-enable the z formatter. Though this is not happening currently.

@ryancwalsh

This comment has been minimized.

Show comment
Hide comment
@ryancwalsh

ryancwalsh Mar 23, 2017

var abbreviation = moment.tz(moment.tz.guess()).format('zz'); doesn't seem to work (with two zs). Am I only able to retrieve "EDT" and not "Eastern Daylight Time"?

Thanks!

ryancwalsh commented Mar 23, 2017

var abbreviation = moment.tz(moment.tz.guess()).format('zz'); doesn't seem to work (with two zs). Am I only able to retrieve "EDT" and not "Eastern Daylight Time"?

Thanks!

@ryancwalsh

This comment has been minimized.

Show comment
Hide comment
@ryancwalsh

ryancwalsh Mar 23, 2017

Ahhh, I found that http://momentjs.com/timezone/docs/#/using-timezones/formatting/ says: To provide long form names, you can override moment.fn.zoneName and use the zz token.
[and much more...]
Thanks.

ryancwalsh commented Mar 23, 2017

Ahhh, I found that http://momentjs.com/timezone/docs/#/using-timezones/formatting/ says: To provide long form names, you can override moment.fn.zoneName and use the zz token.
[and much more...]
Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment