JavaScript日期未定义/ IE8中的NaN [英] JavaScript Date Undefined/NaN in IE8

查看:148
本文介绍了JavaScript日期未定义/ IE8中的NaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个来测试日期输入是否从今天起在所有浏览器中运行的日期不到7天, 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);

我没有在IE控制台(F12)中收到错误,但是日期调试为 NaN ,有没有人知道IE有什么问题?

解决方案

IE 8(及以下)不会在 ymd 格式中识别日期字符串。



我建议您使用 y / m / d ,因为所有浏览器都可以识别。




PS:您的评论是错误的。

  new Date(<?= date(Ymd)?>); // eg:02-10-2012 

应该阅读

  new Date(<?= date(Ymd)?>); // eg:2012-10-02 

一般来说,你不应该将明显的避免没有反映代码的注释。 date(Y-m-d)很明显,根本不需要评论。


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?

解决方案

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.

这篇关于JavaScript日期未定义/ IE8中的NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆