1

I've been banging my head over this one all day. No matter how I initialize a Javascript Date I cannot seem to get a valid Date object... I'm assuming the Date is invalid and not working properly by inspecting it with Chrome's debugger, which has the value '__proto__: Invalid Date'.

I've tried all of the following:

var d = new Date();
var d = new Date('2012-10-08');
var d = new Date('2012-10-08 00:00:00');
var d = new Date(Date('2012-10-08'));
var d = new Date(Date.parse('2012-10-08'));
var d = new Date(2012,10,08);
var d = new Date("October 13, 1975 11:13:00");

Along with countless other attempts.

This is presenting a problem in iOS where I'm trying to get values from these Date objects but every function just returns NaN. I'd prefer to avoid having to use external libraries or have to convert YYYY-MM-DD format into any other format since I'm trying to get this to work with an HTML5 input type="date" with minimal code for a mobile site.

Essentially this boils down to: What are the parameters that make a Date object valid?!

asked Oct 9, 2012 at 22:46
2

4 Answers 4

4

Do not trust the Date object to parse strings, you must do it manually. Given the format 2012年10月08日,

function stringToDate(s) {
 s = s.match(/\d+/g);
 if (s) {
 return new Date(s[0], --s[1], s[2]);
 } 
}

You may want to do some validation of the input string and the resulting date object, the above just shows the conversion.

Edit

BTW, the only string format that seems to be parsed consistently in all browsers is the US-specific month/date/year format. There is no specification to support its use, nor is there any reason to believe browsers will continue to support it other than pragmatism and "legacy" reasons.

For the vast majority of regions, '2/3/2012' is interpreted as 2 March, so getting 3 February might be unexpected.

Once older versions of IE are no longer in use (probably a few years yet), it should be safe to use the ISO8601 extended format per ECMA-262. However, even browsers that support it are inconsitent. e.g given:

new Date('2011-02-29');

Firefox 15 returns 'Invalid Date', IE 9 and Chrome 22 return a date object for 1 March, 2011.

answered Oct 9, 2012 at 22:59
Sign up to request clarification or add additional context in comments.

3 Comments

Can you pass strings to the Date constructor, or do you have to convert them to integers?
Per ECMA-262, each argument is converted to a number using the internal abstract ToNumber operation. So the arguments can be any expression or value.
This produces the wrong Date value. jsfiddle.net/tHQWt (Input value of '2012年10月08日' produces Sun Dec 01 1901 in FF.)
1

There are three ways of calling the method:

  • The number of milliseconds from the epoch:

     new Date(milliseconds)
    
  • Any IETF-compliant RFC 2822 timestamp:

     new Date("November 2, 1988 10:00:00");
    
  • Individual args:

     new Date(year, month, day [, hour, minute, second, millisecond])
     new Date(1988,11,02,10,0,0); 
    
answered Oct 9, 2012 at 22:58

4 Comments

That is plain wrong. The only string that ECMA-262 specifies as being supported is a version of the IS08601 long format, and not all browsers in use support it. new Date(1988,11,02) will become 2 December 1988.
@RobG MDN says "A string representing an RFC2822 or ISO 8601 date." in the Date.parse documentation. ES3 doesn't seem to specify a particular format - the ISO8601 requirement was introduced in ES5.
@Dennis—while MDN is good for examples or proprietary Mozilla features, it's not a specification. It's a community wiki, essentially of observed behaviour. The only authority for ECMAScript that is applicable to browsers (and ECMAScript implementations in general) is ECMA-262.
@RobG I'm saying that there was no spec for this until ES5. Prior to its release, the only authority was observation. MDN has plenty of documentation on support for other browsers, but apparently not in this case. MSDN has no documentation other than "IE<9 doesn't support 8601."
1

If your main concern is about parsing, have a look at Moment.js which is clearly superior to parsing things yourself. (IMHO)

answered Oct 9, 2012 at 23:19

1 Comment

To parse strings consistently with Moment.js you must tell it what the format is. Once you do that, you might as well parse it yourself (2 lines of code).
0

Turns out jQuery doesn't bind the .change() event to input type="date" properly in iOS. Changed the event to .blur() and everything seems to work now. However, it still seems it is impossible to create a valid date object in Chrome... an issue for another day.

Thanks for the help everyone!

answered Oct 10, 2012 at 15:59

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.