This is output from IE10s console after my trying to convert a string to date.
>> new Date("1212-12-12T11:23:00.000+0000")
Invalid Date
Add to watch
Dose any one know if this is a bug or why it works like this in IE10? How can i get this to work?
In chrome and firefox I can simply call
>> new Date("1212-12-12T11:23:00.000+0000")
"Wed Dec 12 1212 12:23:00 GMT+0100 (W. Europe Standard Time)"
Related
I am trying to display a date in javascript. I receive the date from backend like this: 2020-09-22T17:10:25Z (from and Instant object in Java).
When I try to call new Date("2020-09-22T17:10:25Z") I get: Tue Sep 22 2020 20:10:25 GMT+0300 (Eastern European Summer Time). The issue with this is that I am not in a GMT+0300 timezone but rather GMT+0200.
When I try to call new Date() on the other hand I get Thu Dec 08 2022 20:34:11 GMT+0200 (Eastern European Standard Time) which is my correct timezone.
My question is, why in the first case I get the GMT+0300 and in the second case I get GMT+0200? The Z in the string I am trying to parse stands for Zulu or zero hour offset, so why does the 2 different approaches use different timezones?
It looks like you are in GMT+2 in winter, but in summer (in September) you are in summer time which is GMT+3
javascript's date() function works off of the time set on your local computer. if it is giving GMT+3, then your computer is set to GMT+3. check your system clock's configuration.
windows: https://kb.wisc.edu/helpdesk/page.php?id=79027
mac: https://support.apple.com/en-ca/guide/mac-help/mchlp2996/mac
linux: https://www.makeuseof.com/how-to-set-date-and-time-linux/
This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 3 years ago.
I have a very strange behavior in my web app. On one page, new Date() parsing works fine while on other pages, it seems like I get a totally different behavior from the new Date() parsing and it completely fails. I have verified that the Date object has not been overwritten extended or modified and I even tried using moment.js and I get the same results when it's converted to a Date object. I have also verified the same behavior in both Chrome and Edge. Any suggestions?
Page 1 - Seems to work fine, when I type into console.
new Date()
Mon Mar 25 2019 12:58:45 GMT-0400 (Eastern Daylight Time)
new Date("3/25/2019 12:29:51 PM")
Mon Mar 25 2019 12:29:51 GMT-0400 (Eastern Daylight Time)
Page 2 - For some reason parsing fails and returns a completely wrong Date value
new Date()
Mon Mar 25 2019 12:58:52 GMT-0400 (Eastern Daylight Time)
new Date("3/25/2019 12:29:51 PM")
Wed Dec 31 1969 19:00:00 GMT-0500 (Eastern Standard Time)
I also noticed that on page 1, Date.parse("3/25/2019 12:29:51 PM") works but when I try the same on page 2, it returns null.
It seems Page 2 was actually using a script called DateJS (so it was modified) that had some bugs in it too, when I updated the version to the latest, all issues were resolved. Thanks Rayon for your help.
Why do I get such differing results with similarly formatted date strings when creating a new date?
CHROME (43.0.2357.134 m) console:
new Date('2014-12-25')
Wed Dec 24 2014 17:00:00 GMT-0700 (Mountain Standard Time)
[Chrome assumes Z with 00:00 (utc) and returns that local time]
new Date('2014-1-25')
Sat Jan 25 2014 00:00:00 GMT-0700 (Mountain Standard Time)
[What?! exact same format (I thought) but returns 25 instead of 24....see next...]
new Date('2014-01-25')
Fri Jan 24 2014 17:00:00 GMT-0700 (Mountain Standard Time)
[...oh, the leading 0 makes it use the logic it used in the first example]
new Date('2014/12/25')
Thu Dec 25 2014 00:00:00 GMT-0700 (Mountain Standard Time)
[using / instead of - results in what I believe most would expect(?): a local time
on the same date specified]
FIREFOX (39.0) console:
new Date('2014-12-25')
Date 2014-12-25T00:00:00.000Z
[different from Chrome]
new Date('2014-1-25')
Invalid Date
[not recognized in Firefox, unlike Chrome]
new Date('2014-01-25')
Date 2014-01-25T00:00:00.000Z
[different from Chrome]
new Date('2014/12/25')
Date 2014-12-25T07:00:00.000Z
The lesson seems to be: IF you're going to use strings in the Date constructor, make sure it's formatted correctly (per ECMAScript standard):
YYYY-MM-DDTHH:mm:ss.sssZ
CHROME:
new Date('2014-12-25T00:00:00.000-07:00')
Thu Dec 25 2014 00:00:00 GMT-0700 (Mountain Standard Time)
FIREFOX:
new Date('2014-12-25T00:00:00.000-07:00')
Date 2014-12-25T07:00:00.000Z
The ECMAScript standard says in 15.9.3.2
If Type(v) is String, then
Parse v as a date, in exactly the same manner as for the parse method (15.9.4.2);
And in 15.9.4.2 it says:
The function first attempts to parse the format of the String according
to the rules called out in Date Time String Format (15.9.1.15). If the
String does not conform to that format the function may fall back to any
implementation-specific heuristics or implementation-specific date formats.
...which says to me, if you don't provide that exact format, Chrome and Firefox and everyone else can interpret the date string how they deem right. (note: there is some leeway on the format, for example, the value of an absent sss field is “000”. see section 15.9.1.15 for more details)
Can someone explain to me why if I paste the following into the consoles of both Chrome and Firefox I get different results
new Date("2014-12-01")
Output in Chrome:
Sun Nov 30 2014 18:00:00 GMT-0600 (CST)
Output in Firefox:
Date 2014-12-01T00:00:00.000Z
Yes, you're reading that correctly. Chrome renders it as a day before.
Each browser will show dates in the console like it wants.
In your case, Firefox seems to use Date.prototype.toISOString under the hood:
new Date("2014-12-01").toISOString(); // "2014-12-01T00:00:00.000Z"
And Chrome seems to use Date.prototype.toString. In my case, on Chrome and in my timezone, I get
new Date("2014-12-01");
// Mon Dec 01 2014 01:00:00 GMT+0100 (Hora estándar romance)
new Date("2014-12-01").toString();
//"Mon Dec 01 2014 01:00:00 GMT+0100 (Hora estándar romance)"
However, note that Chrome does not render it as a day before. It's just that it uses GMT-0600:
Date.parse("Sun Nov 30 2014 18:00:00 GMT-0600 (CST)"); // 1417392000000
Date.parse("2014-12-01T00:00:00.000Z"); // 1417392000000
I think the Chrome's reading has to do with your locale settings as it converts the GMT entry to your time zone and makes the necessary adjustments whether subtracting, adding, or leaving it as is.
I am passing this new Date into both Firefox and Chrome console (same computer, and time zone) and I am getting mixed results. Chrome is pushing the time forward to my time zone and Firefox is using the passed in time! So confusing...
Firefox
new Date("2014-02-27T17:00:00Z") // Passing in Console
// Result: Date 2014-02-27T17:00:00.000Z
Chrome / Safari
new Date("2014-02-27T17:00:00Z") // Passing in Console
// Result: Thu Feb 27 2014 18:00:00 GMT+0100 (CET)
It is 1 hour in the difference off. Chrome says the time is 18:00:00 while FF says the time is 17:00:00 which is what I expected seen as I formatted to Zulu (UTC) time.
Any help on how to get a consistent date on what is passed in to all browsers?
Thanks
As you can see, Chrome gives you a local time (GMT+0100). That why the hour part is different.
You can try converting to UTC string.
new Date("2014-02-27T17:00:00Z").toUTCString()
// both Chrome and Firefox will give you "Thu, 27 Feb 2014 17:00:00 GMT"