Skip to content Skip to sidebar Skip to footer

Converting Date Strings Into Date Objects Using Dashes Instead Of Slashes Produces Inconsistent Results

I'm trying to convert javascript date strings into date objects. It seems that when I format the strings with slashes, like 2010/05/21, I get the date object I was expecting, but w

Solution 1:

2010/05/21 is a non-ISO date format, so support will be browser implementation dependent. Some browsers may reject it, others will accept it but use different time zones. It looks like your browser is parsing 2010/05/21 with your local time zone.

2010-05-21 is in a simplified ISO 8601 format, so ES5+ has specifications for how it must be parsed. In particular, it must assume the UTC time zone.

You can verify that it's using your local time zone by comparing it to how your browser parses an ISO 8601 date and time (which the ES5 specification says must use the local time zone).

var dateNonISO = newDate('2010/05/21');
var dateLocal = newDate('2010-05-21T00:00:00');
var dateUTC = newDate('2010-05-21');

console.log("Non-ISO:", dateNonISO.toISOString());
console.log("ISO Local:", dateLocal.toISOString());
console.log("ISO UTC:", dateUTC.toISOString());

Post a Comment for "Converting Date Strings Into Date Objects Using Dashes Instead Of Slashes Produces Inconsistent Results"