Case One:
new Date(Date.parse("Jul 8, 2005"));
Output:
Fri Jul 08 2005 00:00:00 GMT-0700 (PST)
Case Two:
new Date(Date.parse("2005-07-08"));
Output:
Thu Jul 07 2005 17:00:00 GMT-0700 (PST)
Why is the second parse incorrect?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Until the 5th edition spec came out, the
Date.parsemethod was completely implementation dependent (new Date(string)is equivalent toDate.parse(string)except the latter returns a number rather than aDate). In the 5th edition spec the requirement was added to support a simplified (and slightly incorrect) ISO-8601 (also see What are valid Date Time Strings in JavaScript?). But other than that, there was no requirement for whatDate.parse/new Date(string)should accept other than that they had to accept whateverDate#toStringoutput (without saying what that was).As of ECMAScript 2017 (edition 8), implementations were required to parse their output for
Date#toStringandDate#toUTCString, but the format of those strings was not specified.As of ECMAScript 2019 (edition 9) the format for
Date#toStringandDate#toUTCString, have been specified as (respectively):e.g. Tue Jul 10 2018 18:39:58 GMT+0530 (IST)
e.g. Tue 10 Jul 2018 13:09:58 GMT
providing 2 more formats that
Date.parseshould parse reliably in new implementations (noting that support is not ubiquitous and non–compliant implementations will remain in use for some time).I would recommend that date strings are parsed manually and the Date constructor used with year, month and day arguments to avoid ambiguity: