javascript - Date (dateString) returning invalid date in Firefox - Stack Overflow

The page works fine in Chrome, but I have this one minor error in Firefox and a different problem in IE

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: .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.

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./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.

Share Improve this question edited Sep 5, 2022 at 18:26 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jul 18, 2010 at 6:35 D-NiceD-Nice 4,87014 gold badges54 silver badges89 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

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

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 ment, 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744854844a4597342.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信