我正在尝试使用一些简单的 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
对于 Chrome。
最佳答案
您的日期格式应为“符合 IETF 标准的 RFC 2822 时间戳”,如果不是,则存在一些跨浏览器不一致的问题。
在这里阅读:http://dygraphs.com/date-formats.html
但基本上 - 您只需将“-
”替换为“/
”即可使其在任何现有浏览器上运行
关于javascript - 为什么这个 Javascript 在 Firefox 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19073368/