JSON.stringify time shift [duplicate] - javascript

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

Related

Calling new Date(String s) produces result in different timezone

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/

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.

what does the T separator in JavaScript Date

I think I need clarification on something:
I have a string representing a date in a format like this:
'2013-12-24 12:30:00'
and if I pass it to Date(), then I get the following output
new Date('2013-12-24 12:30:00')
// --> Tue Dec 24 2013 12:30:00 GMT+0100
because iOS has problems with this, I read that I should use T as separator, however
new Date('2013-12-24T12:30:00')
// --> Tue Dec 24 2013 13:30:00 GMT+0100
the result adds one hour. I guess it has something to do with summer or winter, but what exactly does the T stand for, and why is the result different? I meanwhile solved my problem by passing separate parameters to the Date but I would still like to know where this extra hour is coming from.
new Date('2013-12-24T12:30:00')
treats the time as UTC, so it's 12:30 in Greenwich and 13:30 in your timezone.
new Date('2013-12-24 12:30:00')
is a Chrome extension (or bug) that doesn't work in other browsers. It treats the time as local, so it's 12:30 in your timezone (GMT+1) and 11:30 in Greenwich.
If you look closely... you will notice adding a T makes the time in 24-hour format.
new Date('2013-12-24T12:30:00')
// --> Tue Dec 24 2013 13:30:00 GMT+0100
Compared to
new Date('2013-12-24 12:30:00')
// --> Tue Dec 24 2013 12:30:00 GMT+0100
I guess its stands for Long time pattern. Refer this for more.

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.

DateJS parsing mystery

I'm using DateJS to parse user-inputted dates, and getting some strange results.
Date.parse("15 Jan 2010") returns Fri Jan 15 00:00:00 EST 2010 (right)
Date.parse("15-Apr-2010") returns Thu Apr 15 00:00:00 EDT 2010 (right)
Date.parse("15 Apr 2010") returns Thu Apr 1 00:00:00 EDT 2010 (wrong)
As far as I can tell, the d MMM yyyy input format works fine for every month except April and August; in those two cases, it returns the first of the month no matter what day is entered. Is this a bug, or is there a logical explanation I'm missing?
Aha: Looks like the version in the "Download" link is a good bit older than the current source. Here's the commit that fixed this bug:
Dan Yoder fixed bug with timeContext pattern where if a date included
"april" or "august", the parser thought the 'a' was the beginning of a time part
(as in am/pm).
The most recent version of the EN-US script is here:
http://code.google.com/p/datejs/source/browse/trunk/build/date-en-US.js
It would be nice if the website linked to this instead of to a zip file that hasn't been updated for a couple of years.

Categories