It is advised to never parse strings using the Date constructor or Date.parse since their behavior can vary significantly between different implementations, leading to unexpected results due to various quirks.
As per ECMA-262, the string "2000-01-01" should ideally be parsed as a valid ISO 8601 date-only format with timezone UTC+0000, but older browsers like IE 8 may struggle with this interpretation, sometimes treating it as a local date instead.
If the string provided is not in the precise ISO 8601 format like "2000-1-1", browsers may resort to different heuristics including returning invalid dates, parsing as a local date for January 1, 2000, or considering it as ISO 8601 and parsing it according to UTC+0000.
Current browsers often exhibit behaviors outlined in points 1 and 2 above when confronted with such scenarios.
To avoid these uncertainties, utilizing a library with a built-in parser that allows specifying the format of the string is recommended. Alternatively, if dealing with a single format, you can create a simple function like the following:
/* Parse ISO 8601 date only form as local
** @param {string} s - string in format YYYY-M[M]-D[D]
** @returns {Date} If date parts out of range, returns invalid date
*/
function parseISOLocal(s) {
var b = s.split(/\D/);
var d = new Date(b[0], --b[1], b[2]);
return isNaN(d) || d.getMonth() != b[1]? new Date(NaN) : d;
}
['2000-1-1','2000-01-01','2000-2-29','1900-2-29'].forEach(function(s) {
console.log(s + ' : ' + parseISOLocal(s).toString());
});