-3

What's the diffience between new Date("2019-05-10") and new Date("2019/05/10")
and why?

I mean new Date("2019-05-10") and new Date("2019/05/10") they are different in the console result

金陆欢
  • 21
  • 3
  • 1
    `new Date(2019/05/10)` === `new Date(40.38)` and `new Date(2019-05-10)` === `new Date(2004)` ... if my maths is right – Jaromanda X May 10 '19 at 03:21
  • `2019/05/10` is a math expression: 3 numbers are divided by each other. – zerkms May 10 '19 at 03:21
  • you must try `new Date(2019*05*10)` ( == `new Date(100950)` ;) see https://stackoverflow.com/questions/8224459/how-to-create-a-date-object-from-string-in-javascript – Mister Jojo May 10 '19 at 03:29
  • ```new Date("2019-05-10")``` and new ```Date("2019/05/10")``` have the same date but different time in my console – 金陆欢 May 10 '19 at 03:46
  • 1
    I agree. The one with hyphens ends up being 00:00:00 UTC (then converted to local time), where the one with slashes is 00:00:00 in your local timezone. No idea why. – Nate May 10 '19 at 03:54

1 Answers1

2

I think there's some inconsistencies in date parsing and I found the following excerpt in mdn.

Parsing of date strings with the Date constructor (and Date.parse, they are equivalent) is strongly discouraged due to browser differences and inconsistencies. Support for RFC 2822 format strings is by convention only. Support for ISO 8601 formats differs in that date-only strings (e.g. "1970-01-01") are treated as UTC, not local.

PrivateOmega
  • 2,509
  • 1
  • 17
  • 27
  • This explanation is consistent with the behavior I described in the comments. +1 – Nate May 10 '19 at 04:03
  • thanks. I found that only using the hyphens in date string could cause the same result both in js and node – 金陆欢 May 10 '19 at 06:03
  • @金陆欢 well `YYYY-MM-DDTHH:mm:ss.sssZ` is the format based on simplification of the ISO 8601 Extended Format. And is the one followed by ECMA standard and hence would be same across different execution engines. – PrivateOmega May 10 '19 at 06:29