有时,当您通过新日期(年,月,日)在IE10和IE11中创建新日期时,返回的值包含时间。例如
new Date(2010,10,7)
有时会导致日期值为: 2010年11月6日23:00
而不是预期值: 2010年11月7日00:00
以下是一些说明此问题的示例代码:
<!DOCTYPE html>
<html>
<body>
<script>
document.write(new Date(2013, 10, 3) + "<br>");
document.write(new Date(2013, 2, 10) + "<br>");
document.write(new Date(2012, 10, 3) + "<br>");
document.write(new Date(2012, 2, 10) + "<br>");
document.write(new Date(2012, 10, 7) + "<br>");
document.write(new Date(2012, 2, 14) + "<br>");
document.write("<br>Checking for new Date() with DST issues: <br>");
for (var working = new Date(2014, 1, 1) ; working.getFullYear() > 2009;)
{
document.write(".");
var newDt = new Date(working.valueOf());
var dayOfMonth = newDt.getDate();
dayOfMonth -= 1;
var nextYear = newDt.getFullYear();
var nextMonth = newDt.getMonth();
var newDate = new Date(nextYear, nextMonth, dayOfMonth);
if (newDate.getHours() > 0)
{
document.write("<br>" + newDate + " from:" + nextYear + "," + nextMonth + "," + dayOfMonth + "<br>");
dayOfMonth--;
newDate = new Date(nextYear, nextMonth, dayOfMonth); //skip over this date
}
if (newDate.getDate() === 1)
{
document.write(".");
}
working = newDate;
}
document.write("<br>done.");
</script>
</body>
</html>
IE10&amp;的输出IE11将如下所示:
Sun Nov 3 00:00:00 CDT 2013
Sun Mar 10 00:00:00 CST 2013
Sat Nov 3 00:00:00 CDT 2012
Sat Mar 10 00:00:00 CST 2012
Wed Nov 7 00:00:00 CST 2012
Wed Mar 14 00:00:00 CDT 2012
Checking for new Date() with DST issues:
............................................................................................
Sat Nov 2 23:00:00 CST 2013 from:2013,10,3
.....................................................................................................................................................................................................................................................
Sun Mar 10 01:00:00 CDT 2013 from:2013,2,10
..................................................................................................................................
Fri Nov 2 23:00:00 CST 2012 from:2012,10,3
.....................................................................................................................................................................................................................................................
Sat Mar 10 01:00:00 CDT 2012 from:2012,2,10
........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
Sat Nov 6 23:00:00 CST 2010 from:2010,10,7
.....................................................................................................................................................................................................................................................
Sun Mar 14 01:00:00 CDT 2010 from:2010,2,14
...........................................................................
done.
在firefox,ie9和chrome中执行的相同代码不会创建带有时间值的日期。 我很确定这是一个浏览器错误,但我没有在其他地方看到它。