为什么这个Javascript在Firefox中不起作用? [英] Why does this Javascript not work in Firefox?
问题描述
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 - > 2013年7月22日12:00:00 GMT + 1000 - > 2013-07-22
Chrome可以正常工作,您可以通过小提琴。它总是返回
无效日期
无效日期
NaN-aN-aN
对于Firefox中的三个 console.logs
,但是:
Tue Jul 23 2013 12:00:00 GMT + 1000(E.澳大利亚标准时间)
2013年7月22日星期一12:00 12:00 GMT + 1000(E.澳大利亚标准时间)
2013-07-22
对于Chrome。
您的日期格式应为符合IETF的RFC 2822时间戳一些跨浏览器不一致,如果不是。
在此阅读: http://dygraphs.com/date-formats.html
但基本上 - 你应该只是替换' -
'与' /
'使其在任何现有的浏览器上工作
I am trying to manipulate a date with some simple Javascript. The code is as follows:
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);
Essentially, I am converting
2013-07-23 -> Jul 22 2013 12:00:00 GMT+1000 -> 2013-07-22
It works fine in Chrome, you can test the code via this Fiddle. It always returns
"Invalid Date"
"Invalid Date"
"NaN-aN-aN"
For the three console.logs
in Firefox, but:
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
For Chrome.
Your date format should be "IETF-compliant RFC 2822 timestamp" and there's some cross-browser inconsistency if it's not.
Read about it here: http://dygraphs.com/date-formats.html
But basically - you should just replace '-
' with '/
' to make it work on any existing browser
这篇关于为什么这个Javascript在Firefox中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!