Monday, June 3, 2024
57
rated 0 times [  64] [ 7]  / answers: 1 / hits: 138635  / 14 Years ago, thu, july 15, 2010, 12:00:00

I am creating a datetime string that looks like this: 2010-07-15 11:54:21



And with the following code I get invalid date in Firefox but works just fine in Chrome



var todayDateTime = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + seconds;
var date1 = new Date(todayDateTime);


In firefox date1 is giving me an invalid date, but in chrome its working just fine what would the main cause be?


More From » google-chrome

 Answers
17

You can't instantiate a date object any way you want. It has to be in a specific way. Here are some valid examples:



new Date() // current date and time
new Date(milliseconds) //milliseconds since 1970/01/01
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)


or



d1 = new Date(October 13, 1975 11:13:00)
d2 = new Date(79,5,24)
d3 = new Date(79,5,24,11,33,0)


Chrome must just be more flexible.



Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date



From apsillers comment:



the EMCAScript specification requires exactly one date format (i.e., YYYY-MM-DDTHH:mm:ss.sssZ) but custom date formats may be freely supported by an implementation: If the String does not conform to that [ECMAScript-defined] format the function may fall back to any implementation-specific heuristics or implementation-specific date formats. Chrome and FF simply have different implementation-specific date formats.


[#96220] Tuesday, July 13, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cruz

Total Points: 415
Total Questions: 98
Total Answers: 109

Location: France
Member since Thu, May 6, 2021
3 Years ago
cruz questions
Tue, Sep 14, 21, 00:00, 3 Years ago
Fri, Mar 26, 21, 00:00, 3 Years ago
Sat, Oct 26, 19, 00:00, 5 Years ago
;