您的位置:首页 > 其它

new Date 在IE 下 兼容性的问题

2013-04-11 17:24 183 查看
问题 : new Date(string) 在IE下不兼容,返回NaN

解决办法: :用 new Date(year,month,day,hour,minute,millseconds)

使用例子 如下:

/*

* 选择最近时间段自动同步到起止时间

*/

function syncDateAuto(curObj){

var beginDate , endDate , curDate = new Date();

var chooseDate = $(curObj).val();

if(chooseDate === "0"){ // 今天

beginDate = curDate;

}else if(chooseDate === "1"){ // 最近7天

beginDate = new Date(curDate.getTime() - 7 * 24 * 60 * 60 * 1000);

}else if(chooseDate === "2"){ // 最近1个月

var year = curDate.getFullYear();

var month = curDate.getMonth() - 1;

var day = curDate.getDate();

beginDate = new Date(year,month,day,00,00,00);

}else if(chooseDate === "3"){

var year = curDate.getFullYear();

var month = curDate.getMonth() - 3;

var day = curDate.getDate();

beginDate = new Date(year,month,day,00,00,00);

}

$("#searchBeginDate").val(beginDate.Format("yyyy-MM-dd"));

$("#searchEndDate").val(curDate.Format("yyyy-MM-dd"));

}

再看看老外的回复:

2
down vote
accepted
Looking to the
documetation the right format is the following:

new Date(year, month, day [, hour, minute, second, millisecond ])

So if you run the following code it will be fine in all browsers:

var myDate= new Date(1985, 01, 01 , 00, 06, 00, 0000000000);
myDate // you get the right date in all browsers IE8/7 included


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: