new Date is formatting differently on Firefox and Chrome - javascript

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"

Related

Javascript Date creation difference - in IE vs chrome

I am getting a long from the backend system that represents a date.
I convert the long to javascript Date by using - new Date(long).
I get different dates for the same long in IE and Chrome.
The correct date is the one shown in Chrome.
example
For example:
new Date(1032382800000)
In IE the date value is - Wed Sep 18 2002 23:00:00 GMT +0200.
In Chrome date value is - Thu Sep 19 2002 00:00:00 GMT +0300.
How can I solve this discrepancy?
I have found a workaround.
Because the time is not relevant in those date objects, I run the following code:
var iTimezoneOffset = dDate.getTimezoneOffset() - (new Date()).getTimezoneOffset();
if (iTimezoneOffset != 0) {
dDate.setMinutes(dDate.getMinutes() + iTimezoneOffset);
}

JSON.stringify time shift [duplicate]

Starting Chrome 67 (full version is 67.0.3396.87), I am experiencing weird behaviour with creation of a new Date object. Smallest reproducible case, goes something like:
<html>
<body>
<script> alert(new Date(-62135596800000)); </script>
</body>
</html>
On Firefox 60.0.2 the alert message is:
Mon Jan 01 0001 00:00:00 GMT+0000 (GMT Standard Time)
On Internet Explorer 11 and Edge 41.16299.461.0, the alert message is same as Firefox:
Mon Jan 01 0001 00:00:00 GMT+0000 (GMT Standard Time)
However, on Chrome 67 I see:
Sun Dec 31 0000 23:58:45 GMT-0001 (Greenwich Mean Time)
Edit: JsFiddle
Edit2 Turns out it's nothing to do with Microsoft's library.
Depending on the response to that bug report, I think this is actually due to Chrome possibly including IANA timezone information. Browsers, time zones, Chrome 67 Error
For example, when I run that fiddle, I get Sun Dec 31 0000 18:09:24 GMT-0550 (Central Standard Time) which corresponds to the IANA entry Zone America/Chicago -5:50:36 - LMT 1883 Nov 18 12:09:24.
So this is a "feature" not a bug I think. They are using the more "accurate" historical time offsets instead of current day time offsets for historical dates.
You can view the data here : https://github.com/eggert/tz just look for your appropriate world location file and try and avoid all the commented out lines unless you are morbidly curious about the history of your time zones.
What you can do to "fix" it so it display more or less correctly is to call .toUTCString() on the Date object which will force it to UTC time and display Mon, 01 Jan 0001 00:00:00 GMT as #Pointy pointed out in the comments on the initial question.
Your code, as long as MS's one, doesn't know anything about the timezone, so I assume Chrome takes the default one.
I guess it should be better if you create your date as below:
const date = new Date(Date.UTC(year, month, day, hour, minute, second));

Difference in how browsers parse dates

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.

Why IE10? Date creation

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)"

Difference between these dates in Javascript? And why do not all browsers give same result?

var dt = new Date("2012-04-23T12:00:00");
var dtz = new Date("2012-04-23T12:00:00Z");
If the Z is present I get a different time.
When the Z is present is it converting the Date to the browser's local time and when not present assuming it is already in local time?
I get different results in FF than Chrome. Chrome always gives me the same time. FF treats them as different. How should I be dealing with UTC dates from the server?
"Z" is a military time zone corresponding to UT (aka UTC, aka GMT). So basically, 'nnn Z' means "how late is it in your time zone when it's 'nnn' in Greenwich". For example, I'm in CEST which is GMT+2 so this
new Date("2012-04-23T12:00:00Z")
returns for me:
Mon Apr 23 2012 14:00:00 GMT+0200 (CEST)
As to dates with a TZ specifier, they seem to be treated differently in Firefox (which assumes local TZ) and Chrome (which assumes UTC). For safety, I'd suggest always using an explicit TZ specifier.
var dt = new Date("2012-04-23T12:00:00");
var dtz = new Date("2012-04-23T12:00:00Z");
tried it with alert() and got these messages
alert(dt);
Mon Apr 23 2012 12:00:00 GMT+0500 (West Asia Standard Time)
alert(dtz);
Mon Apr 23 2012 17:00:00 GMT+0500 (West Asia Standard Time)
it means that if you create the date without "Z", it returns browsers's local time at GMT, mentioning your time zone is below or above GMT
and if you create it with "Z", it will show the local time at your time zone, referring to your time zone.
According to ISO 8601, IF no UTC relation information is given with a time representation, the time is assumed to be in local time.
If can verify the correct behavior on both Firefox, Safari and Internet Explorer:
The following should return: false
new Date("2014-05-09T22:12:18.893Z").valueOf() === new Date("2014-05-09T22:12:18.893").valueOf()
If you try the same thing on Chrome or Opera, it will incorrect indicate: true
The moral of the story is, if you have a string in the above format, add a Z at the end.

Categories