If I try to parse a date with this syntax:
var date1 = new Date(Date.parse('2013' + '/' + '02' + '/' + '29'));
it will return 1 March 2013.
var date1 = new Date(Date.parse('2013' + '/' + '02' + '/' + '30'));
it will return 2 March 2013. But if I do
var date1 = new Date(Date.parse('2013' + '/' + '02' + '/' + '33'));
it will return Invalid Date
.
My point is, why don't all of these dates return Invalid Date
?
If the DATEPARSE function is not available for the data that you're working with, or the field you are trying to convert is a number data type, you can use the DATE function instead. The DATE function converts a number, string or date expression to a date type.
The Date.parse() method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognized or, in some cases, contains illegal date values (e.g. 2015-02-31).
Definition and Usage parse() parses a date string and returns the time difference since January 1, 1970. parse() returns the time difference in milliseconds.
The only format that Date.parse
is required to parse correctly is a simplification of ISO 8601. You can read more details about the exact format in the ecma specification.
Any other format that Date.parse
recognizes as a date is implementation specific. The format you mentioned is not part of the standard above, so each implementation can give whatever result for it they wish.
It so happens that when you pass in what looks like a day of the month larger than 31, the parser will consider it to be an invalid string, so it will return NaN
. Checking to see if the date is valid is a lot more difficult due to various issues with month-day irregularities, leap years, missing seconds, timezones, etc, so placing all of that logic in the parser is not warranted. With the date apparently valid, it is converted to a timestamp and returned by Date.parse
and at that point new Date() has something to work with.
The conclusion is that using non-standard formats with Date.parse
is unreliable and should be avoided if possible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With