admin管理员组文章数量:1333704
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.
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.
Share Improve this question asked Sep 29, 2013 at 0:58 JosephGarroneJosephGarrone 4,1613 gold badges41 silver badges61 bronze badges3 Answers
Reset to default 5Your date format should be "IETF-pliant RFC 2822 timestamp" and there's some cross-browser inconsistency if it's not.
Read about it here: http://dygraphs./date-formats.html
But basically - you should just replace '-
' with '/
' to make it work on any existing browser
The spec does not require that the date format YYYY-MM-DD HH:MM:SS
be parsed:
new Date
:
http://www.ecma-international/ecma-262/5.1/#sec-15.9.3.2
Parse v as a date, in exactly the same manner as for the parse method
(essentially, the same result as Date.parse(...)
)
Date.parse
: http://www.ecma-international/ecma-262/5.1/#sec-15.9.4.2
The function first attempts to parse the format of the String according to the rules called out in Date Time String Format (15.9.1.15). If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats. Unrecognisable Strings or dates containing illegal element values in the format String shall cause Date.parse to return NaN.
Date Time String Format: http://www.ecma-international/ecma-262/5.1/#sec-15.9.1.15
The format is as follows: YYYY-MM-DDTHH:mm:ss.sssZ
There is no guarantee that any other format works, including YYYY-MM-DD HH:MM:SS
.
You can always construct a conformant date string:
var newDate = new Date("2013-07-23" + "T12:00:00.000Z");
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);
In Firefox:
new Date("2013-07-23 12:00:00").toString() // Invalid
new Date("2013-07-23T12:00:00").toString() // Local noon
new Date("2013-07-23T12:00:00Z").toString() // UTC noon
new Date("2013/07/23 12:00:00").toString() // Local noon
In Chrome:
new Date("2013-07-23 12:00:00").toString() // Local noon
new Date("2013-07-23T12:00:00").toString() // UTC noon
new Date("2013-07-23T12:00:00Z").toString() // UTC noon
new Date("2013/07/23 12:00:00").toString() // Local noon
There are many other inconsistencies. For example, Chrome isn't even consistent within itself:
new Date("2013-07-23 00:00:00").toString() // Local midnight
new Date("2013-07-23").toString() // UTC midnight
If you need to parse dates from strings in a consistent manner, you should consider using a library such as moment.js.
moment("2013-07-23 12:00:00", "YYYY-MM-DD HH:mm:ss").format() // Local noon
moment.utc("2013-07-23 12:00:00", "YYYY-MM-DD HH:mm:ss").format() // UTC noon
The main advantage is that you can control the input and output formats, and it works the same way in all browsers.
本文标签: dateWhy does this Javascript not work in FirefoxStack Overflow
版权声明:本文标题:date - Why does this Javascript not work in Firefox? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742271355a2444364.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论