我试图用一些简单的Javascript来操纵日期.代码如下:
var newDate = new Date("2013-07-23" + " 12:00:00");
console.log(newDate.toString());
newDate = new Date(newDate.getTime() - (24 * 60 * 60 * 1000));
console.log(newDate.toString());
var date = newDate.getFullYear() + "-" + ("0" + (newDate.getMonth() + 1)).slice(-2) + "-" + ("0" + newDate.getDate()).slice(-2);
console.log(date);
Run Code Online (Sandbox Code Playgroud)
基本上,我正在转换
2013-07-23 -> Jul 22 2013 12:00:00 GMT+1000 -> 2013-07-22
Run Code Online (Sandbox Code Playgroud)
它在Chrome中运行良好,你可以通过这个小提琴测试代码.它总是回归
"Invalid Date"
"Invalid Date"
"NaN-aN-aN"
Run Code Online (Sandbox Code Playgroud)
对于console.logsFirefox中的三个,但是:
Tue Jul 23 2013 12:00:00 GMT+1000 (E. Australia Standard Time)
Mon Jul 22 2013 12:00:00 GMT+1000 (E. Australia Standard Time)
2013-07-22
Run Code Online (Sandbox Code Playgroud)
适用于Chrome.
您的日期格式应为"符合IETF的RFC 2822时间戳",如果不是,则存在一些跨浏览器的不一致.
在这里阅读:http://dygraphs.com/date-formats.html
但基本上 - 您应该将' -' 替换为' /'以使其适用于任何现有的浏览器