-
Notifications
You must be signed in to change notification settings - Fork 7k
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
Comments
Hmm, looks like the timezone abbreviation is failing. Sorry to do remote debugging, but what is the output of the following in chrome?
Also, what is the name of the timezone you are in? |
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 |
Hmm, I was afraid of this. The problem is that Perhaps it's time to depreciate? Or is it acceptable to just note the issue in the docs? |
I also get the same failing test # 28. My timezone is GMT -3. Agent: Failed test: http://pastebin.com/dsWHMF3h .:. Agent: Failed test: http://pastebin.com/6t6JP9fu |
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 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. |
The |
What about node.js ? Are |
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. |
Thanks. So for a string like |
Hmm, I'm not sure what you are trying to do, are you trying to get
|
I'm doing as you say, using ZZ for +0100 and removing CET from the string to parse. Does it make sense ? |
My apologies, I'm still not understanding what the problem is here. Are you trying to parse moment('Thu Jan 10 2013 22:54:11 GMT+0100 (CET)', 'ddd MMM DD YYYY HH:mm:ss [GMT]ZZ'); |
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 ! |
No problem! |
@hughanderson4, the issue was that browsers don't always return a timezone abbreviation from 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. |
How would one convert a moment.utc() to local time and display the time zone abbreviation? |
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:
with:
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. |
So, asking just to make sure I understand: There is no replacement for the deprecated 'z' format? |
If I have a timestamp in the ISO-8601 format, such as:
And I want to display this timestamp as the following:
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. |
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. |
For clarification, the |
Just mentioning this now - if you use This means that you could possibly use this string to extract the timezone, no? |
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. |
@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. |
@Shobana16 - If you just do If you're using moment-timezone, and do something like
|
Thank you for the comments MJ. Yeah i am using 'z' with moment-timezone only. Ok cool will remain my code with 'z'. ! |
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:
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 |
Thanks! |
Ahhh, I found that http://momentjs.com/timezone/docs/#/using-timezones/formatting/ says: To provide long form names, you can override |
One thing is confusing though. In current version (or as it is stated "as of 1.6.0") there is no way to show timezone without using separate moment-timezone. But when I do |
I think the problem is reliability. Moment can't reliably extract the correct timezone from the local date object so it no longer supports Discussed above: |
This is what worked for us to get the "GMT" text since const dateGenerated = moment("2020-05-20 08:15:25");
const localDateGenerated = dateGenerated.local().format('DD MMM YYYY hh:mm A') + ' ' + String(dateGenerated.local()._d).split(' ')[5]; It will generate |
How do i add +1 to hours ?? Iam using this: but i want to add +1 to hours to match my local time |
This has nothing to do with the timezone display format. You need to use local() as in moment().local().format(''); |
well it does not work in my code at all, i use loop for many accounts, so the muldule only print the first time and keep repeating it no update. |
I confirm that |
Using: "moment": "^2.29.4",
"moment-timezone": "^0.5.43" This works: const timezone = "America/New_York";
const timezoneAbbreviation = moment.tz(timezone).zoneAbbr();
const formattedDate = moment(date, timezone).format('...') + timezoneAbbreviation; |
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
The text was updated successfully, but these errors were encountered: