Reputation: 57695
Why does,
new Date('2014-04-16')
create a date object (note Apr 15, and 17:00):
Tue Apr 15 2014 17:00:00 GMT-0700 (PDT)
While
new Date('2014-4-16')
Wed Apr 16 2014 00:00:00 GMT-0700 (PDT)
and
new Date('2014/04/16')
Wed Apr 16 2014 00:00:00 GMT-0700 (PDT)
http://jsfiddle.net/pajtai/5LrGX/
Noticed this in Chrome.
Is this something to do with ISO date formatting?
Upvotes: 0
Views: 96
Reputation: 92304
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
The constructor allows the following for a string argument
dateString
String value representing a date. The string should be in a format recognized by the >Date.parse() method (IETF-compliant RFC 2822 timestamps and also a version of ISO8601).
So if it's not one of those formats, you shouldn't rely on it. Both of the formats mentioned have two digits for month and day and use dashes, not slashes
You'll notice that Firefox and IE do not allow your month without 2 digits
// Firefox and IE
new Date('2014-4-16').toString()
> 'Invalid Date'
Upvotes: 1