我正在尝试使用一些简单的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);
基本上,我正在转换
2013-07-23 -> Jul 22 2013 12:00:00 GMT+1000 -> 2013-07-22
在Chrome中它能够正常工作,你可以通过这个Fiddle测试代码。它总是返回。
"Invalid Date"
"Invalid Date"
"NaN-aN-aN"
针对Firefox中的三个console.logs
,但是:
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
谷歌浏览器适用。