D-Nice
D-Nice

Reputation: 4870

Date (dateString) returning invalid date in Firefox

The page works fine in Chrome, but I have this one minor error in Firefox and a different problem in IE. Assistance with either of these issues is greatly appreciated. Since I've been stumped in the Firefox error the longest, I'll start with that one:

Here's the code: http://truxmapper.appspot.com/sched.html

The date picker selects a date using the format "07-08-2010 23:28". Now, I need to pass this time as a parameter to my servlet, which is expecting the time represented as a long. This is not a problem in Chrome. The Date object accepts a string in the format given above, but when I try to use getTime() on a date instantiated with a string in Firefox, it returns NaN. So what I've done in the on the page I linked to is a little handling asking the user to re-enter the dates if its read as NaN. This obviously isn't even a band-aid solution since even if you re-enter the date its still going to read NaN. I need to know why the Date function wont instantiate using the string you see in the input text field in Firefox.

In IE, for some reason its telling me that sTime is undefined.

Upvotes: 2

Views: 3203

Answers (2)

kennytm
kennytm

Reputation: 523224

Try

new Date(Date(dateString)).getTime()

(feels like an ugly workaround...)

Edit: This will produce wrong result.


The date format used in Javascript should be of the form YYYY MM DD HH:mm:ss. You can convert the format into this form with

// dateString = "07-08-2010 23:28";
dateString = dateString.replace(/(\d+) (\d+) (\d+)/, '$3-$1-$2');

But as mentioned in the comment, there is no standard Date format used by Javascript before the ECMAScript 5 standard. It is better to parse the dateString directly:

m = dateString.match(/(\d+)-(\d+)-(\d+) (\d+):(\d+)/)
date = new Date(+m[3], m[1]-1, +m[2], +m[4], +m[5]); // Note: January = 0.

Upvotes: 3

Dagg Nabbit
Dagg Nabbit

Reputation: 76736

That date format is ambiguous. Try it as yyyy-mm-dd instead of mm-dd-yyyy or dd-mm-yyyy.

Upvotes: 5

Related Questions