Typescript: Creating new Date from string date adds 21 seconds to result

Created on 5 Jul 2019  ·  4Comments  ·  Source: microsoft/TypeScript


TypeScript Version: 3.5.2


Search Terms: 3.5.2 date 21 seconds new Date() new Date string greenwich France

Code

Creating new dates (before or equal to 10 march 1911) from string format dates adds 21 seconds to the resulting date.


var firstDate = new Date("1900-01-01T00:00:00.000Z").toString()
// Mon Jan 01 1900 00:09:21 GMT+0009 (Central European Standard Time)

var secondDate = new Date(firstDate).toString()
// Mon Jan 01 1900 00:09:42 GMT+0009 (Central European Standard Time)

var thirdDate = new Date(secondDate).toString()
// Mon Jan 01 1900 00:10:03 GMT+0009 (Central European Standard Time)

Expected behavior:
Should return Mon Jan 01 1900 00:09:21 GMT+0009 (Central European Standard Time)

Actual behavior:
Adds 21 seconds every time new Date(string) is called.

Playground Link:

Related Issues:

Question

Most helpful comment

\

@j-oliveras it all depends on your compiler options:

temporalAnomaly

<\/sarcasm>

All 4 comments

How is this an error with typescript when this is on runtime?

Next, the toString is lowercased. With Node (10.15.3), Firefox (67.0.4) devtools console and Chrome (75) devtools console this it works as expected (comments from node output):

var firstDate = new Date("1900-01-01T00:00:00.000Z").toString()
// Mon Jan 01 1900 01:00:00 GMT+0100 (GMT+01:00)

var secondDate = new Date(firstDate).toString()
// Mon Jan 01 1900 01:00:00 GMT+0100 (GMT+01:00)

var thirdDate = new Date(secondDate).toString()
// Mon Jan 01 1900 01:00:00 GMT+0100 (GMT+01:00)

\

@j-oliveras it all depends on your compiler options:

temporalAnomaly

<\/sarcasm>

In all seriousness, @ruslan-volkov, this issue does not concern the TypeScript compiler. As long as the compiler produces the right JavaScript output code, which it does, there's nothing wrong with TypeScript.

What you are seeing might well be a bug in a particular JavaScript runtime engine (or maybe multiple engines), likely having to do with calculating dates before 1911 in France when the country was 9 minutes and 21 seconds ahead of GMT. But it has nothing to do with TypeScript.

I'm also not sure if anyone in the JS runtime world will care much about this, given all the issues with creating Date objects the way you are doing it:

Note: parsing of date strings with the Date constructor ... is strongly discouraged due to browser differences and inconsistencies.

Good luck!

Thanks for your answers and your time, my bad !

Was this page helpful?
0 / 5 - 0 ratings