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?!
-
3developer.mozilla.org/en-US/docs/JavaScript/Reference/…Dennis– Dennis2012年10月09日 22:48:30 +00:00Commented Oct 9, 2012 at 22:48
-
You could try reading ECMA-262 §15.9.3.RobG– RobG2012年10月09日 23:07:23 +00:00Commented Oct 9, 2012 at 23:07
4 Answers 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.
3 Comments
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);
4 Comments
new Date(1988,11,02) will become 2 December 1988."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.If your main concern is about parsing, have a look at Moment.js which is clearly superior to parsing things yourself. (IMHO)
1 Comment
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!