I have textbox( id='txtCalendarSeventh' ) which have a calendar control associated with it. On Selecting any date from calendar I get the value in textbox in the format- Apr-21-2014 .

I am using the following javascript code-

 var dateresult = document.getElementById('txtCalendarSeventh').value;
    var d = new Date(dateresult);
    alert(d);

This code works fine in chrome and dispalys correct value of date. But in firefox or internet explorer it is showing Nan.Nan.Nan etc instead of correct date.

有帮助吗?

解决方案

"Apr-21-2014" is not a valid date string, although some browsers may recognise it. "Apr 21 2014" should work though, for example.

See http://msdn.microsoft.com/en-us/library/az4se3k1%28v=vs.110%29.aspx for a comprehensive list.

其他提示

The best you can do is use the ISO format: YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS

For example:

new Date('2011-04-11')

or

new Date('2011-04-11T11:51:00')

For more Info: MDN | Date

Edit:

For old Internet Explorer compatibility (IE versions less than 9 do not support ISO format in Date constructor), you should split datetime string representation to it's parts and then you can use constructor using datetime parts, e.g.: new Date('2011', '04' - 1, '11', '11', '51', '00')

Note that the number of the month must be 1 less.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top