Jim SMith
Jim SMith

Reputation: 212

JavaScript Date Undefined/NaN in IE8

I use this to test whether date inputs were less than 7 days from today's date it works in all browsers except < IE9

var today = new Date("<?=date("Y-m-d")?>"); //eg: 02-10-2012
var arrDate = new Date(startYear+"-"+startMonth+"-"+startDay); //eg: 05-10-2012
var diff = new Date(arrDate - today);
var days = diff/1000/60/60/24;
if(days<7) alert("less than 7 days.");

I don't get an error in the IE console (F12) but days debugs as NaN, does anybody know what the problem is with IE?

Upvotes: 3

Views: 5904

Answers (1)

Tomalak
Tomalak

Reputation: 338178

IE 8 (and below) do not recognize date strings in the y-m-d format.

I recommend you use y/m/d, as this is recognized by all browsers.


PS: Your comment is wrong.

new Date("<?=date("Y-m-d")?>"); //eg: 02-10-2012

should read

new Date("<?=date("Y-m-d")?>"); //eg: 2012-10-02

On a general note, you should never comment the obvious to avoid comments that do not reflect the code. date("Y-m-d") is pretty obvious, it does not need a comment at all.

Upvotes: 15

Related Questions