1

Why there difference in output dates, while I didn't specify timezone in both.

new Date("2020-06-01 00:00:00") Date Mon Jun 01 2020 00:00:00 GMT+0900 (日本標準時)

new Date("2020-06-01") Date Mon Jun 01 2020 09:00:00 GMT+0900 (日本標準時)

both Chrome and Firefox prints the same. Thank you. ​

sverek
  • 51
  • 3
  • "Why" may be difficult to answer, but https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date says that this is the case. (Search within that page for `date-only strings`.) – ruakh May 19 '20 at 07:48
  • Found from related answers: https://stackoverflow.com/a/30199268/11349804 ... parsing is hard. – sverek May 19 '20 at 07:50
  • Check the answers in the duplicate. [This one](https://stackoverflow.com/a/20463521) explains different string format parsing. `YYYY-MM-DD` is ISO 8601 date–only format. So, UTC is used. If you change the first one to `new Date("2020-06-01T00:00:00Z")`, it should return UTC as well – adiga May 19 '20 at 07:53
  • Actually, when you pass `hh:mm:ss` to the `constructor` it sets `GMT` time based on your location. And when you don't pass them, `GMT` time is `00:00:00` and `hours` are set based on your location, in this example, `GMT+0900`, that's why the time is `09:00:00` – Guga Nemsitsveridze May 19 '20 at 07:55
  • I think that question shouldn't be closed because it isn't about parsing; That was not a thing. – Guga Nemsitsveridze May 19 '20 at 07:56
  • 1
    @GugaNemsitsveridze, no it explains it very well in related posts. "As a general rule, if a browser can interpret a date as an ISO-8601, it will. "2005-07-08" falls into this camp, and so it is parsed as UTC." - https://stackoverflow.com/a/9709060/11349804 And new Date is basically does parsing of gives value. – sverek May 19 '20 at 08:01

0 Answers0