Siphon
Siphon

Reputation: 1061

JS Date() - Leading zero on days

A leading zero for the day within a string seems to break the Javascript Date object in Chrome. There are also some inconsistencies between browsers, since Firefox handles the leading zero correctly, but fails when the zero is not included. See this example: https://jsfiddle.net/3m6ovh1f/3/

Date('2015-11-01'); // works in Firefox, not in Chrome

Date('2015-11-1'); // works in Chrome, not in Firefox

Why? Is there a good way to work around/with the leading zero?

Please note, the strings are coming from MySQL via AJAX and all dates will contain the leading zero, and I can fix this by formating the dates server-side. What format would work the best?

EDIT

Just to specify what my problem was, it looks like Chrome is applying a time zone to the YYYY-MM-DD format, which reverts the Nov. 1st date back to the Oct. 31st date (because of my EDT local time).

Upvotes: 5

Views: 1857

Answers (2)

hichris123
hichris123

Reputation: 10233

According to ECMA-262 (5.1):

The function first attempts to parse the format of the String according to the rules called out in Date Time String Format (15.9.1.15). If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats.

The date/time string format as described in 15.9.1.15 is YYYY-MM-DDTHH:mm:ss.sssZ. It can also be a shorter representation of this format, like YYYY-MM-DD.

2015-11-1 is not a valid date/time string for Javascript (note it's YYYY-MM-D and not YYYY-MM-DD). Thus, the implementation (browser) is able to do whatever it wants with that string. It can attempt to parse the string in a different format, or it can simply say that the string is an invalid date. Chrome chooses the former (see DateParser::Parse) and attempts to parse it as a "legacy" date. Firefox seems to choose the latter, and refuses to parse it.

Now, your claim that new Date('2015-11-01') doesn't work in Chrome is incorrect. As the string conforms to the date/time string format, Chrome must parse it to be specification compliant. In fact, I just tried it myself -- it works in Chrome.

So, what are your options here?

  1. Use the correct date/time format (i.e. YYYY-MM-DD or some extension of it).

  2. Use the new Date (year, month, date) constructor, i.e. new Date(2015, 10, 1) (months go from 0-11) in this case.

Whichever option is up to you, but there is a date/time string format that all specification compliant browsers should agree on.

Upvotes: 7

jpaljasma
jpaljasma

Reputation: 1612

As an alternative, why not use unix timestamps instead? In JavaScript, you would would multiply the timestamp value by 1000, e.g

var _t = { time: 1446220558 };
var _d = new Date( _t.time*1000 );

Test in your browser console:

new Date( 14462205581000 );
// prints Fri Oct 30 2015 11:55:58 GMT-0400 (EDT)

There's a little benefit in it as well (if data comes via JS) - you'd save 2 bytes on every date element '2015-10-30' VS 1446220558 :)

Upvotes: 4

Related Questions