可能重复:
IE JavaScript 日期解析错误
此代码在 chrome 和 firefox 中运行良好,但在 IE 和 safari 中不起作用。它在 IE 中返回 NAN,在 safair 中返回无效日期。
var date = new Date("2012-10-17T08:15:19.500-05:00");
var now = new Date();
var difference = now - date;
document.write( "Date: " + date.toLocaleString() + "<br/>");
document.write( "Now: " + now.toLocaleString() + "<br/>");
document.write( "Difference: " + differenceToString(difference) );
function differenceToString(milliseconds) {
var seconds = milliseconds / 1000;
var numyears = Math.floor(seconds / 31536000);
var numdays = Math.floor((seconds % 31536000) / 86400);
var numhours = Math.floor(((seconds % 31536000) % 86400) / 3600);
var numminutes = Math.floor((((seconds % 31536000) % 86400) % 3600) / 60);
var numseconds = Math.floor((((seconds % 31536000) % 86400) % 3600) % 60);
return numyears + " years, " + numdays + " days, " + numhours + " hours, " + numminutes + " minutes, " + numseconds + " seconds";
}
任何想法都会有很大帮助。
谢谢