Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
35
rated 0 times [  39] [ 4]  / answers: 1 / hits: 100886  / 14 Years ago, sun, march 21, 2010, 12:00:00

The following script returns 20 instead of 21!



var d = new Date(2010/03/21);
document.write(d.getDate());


What am I doing wrong? Is this a JavaScript bug?


More From » getdate

 Answers
28

The Date.parse method is implementation dependent (new Date(string) is equivalent to Date.parse(string)).



While this format will be available on modern browsers, you cannot be 100% sure that the browser will interpret exactly your desired format.



I would recommend you to manipulate your string, and use the Date constructor with the year, month and day arguments:



// parse a date in yyyy-mm-dd format
function parseDate(input) {
var parts = input.match(/(d+)/g);
// new Date(year, month [, date [, hours[, minutes[, seconds[, ms]]]]])
return new Date(parts[0], parts[1]-1, parts[2]); // months are 0-based
}

[#97277] Wednesday, March 17, 2010, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dominiquezachariahh

Total Points: 343
Total Questions: 81
Total Answers: 106

Location: Monaco
Member since Fri, Sep 24, 2021
3 Years ago
;