Skip to content
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

Creating new Date from string date adds 21 seconds to result #32265

Closed
ruslan-volkov opened this issue Jul 5, 2019 · 4 comments
Closed

Creating new Date from string date adds 21 seconds to result #32265

ruslan-volkov opened this issue Jul 5, 2019 · 4 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@ruslan-volkov
Copy link

ruslan-volkov commented Jul 5, 2019

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:

@j-oliveras
Copy link
Contributor

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)

@jcalz
Copy link
Contributor

jcalz commented Jul 5, 2019

<sarcasm>

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

temporalAnomaly

</sarcasm>

@jcalz
Copy link
Contributor

jcalz commented Jul 8, 2019

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!

@ruslan-volkov
Copy link
Author

Thanks for your answers and your time, my bad !

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Jul 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants