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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742271356a4412758.html
评论列表(0条)