Javascript: difference between `new Date (dateString)` vs `new Date (year, month, day)`

Link to accepted answer on this question How to get the number of days between two dates in JavaScript? . I see in the function parseDate:

function parseDate(str) {
    var mdy = str.split('/')
    return new Date(mdy[2], mdy[0]-1, mdy[1]);
}

He does this:

var mdy = str.split('/')
return new Date(mdy[2], mdy[0]-1, mdy[1]);

i.e. dividing the past date into a month, day and year, and then passing it to Date, like new Date(year, month, day), while he could just do new Date(str)it and he would return the same result (right?). Can someone explain the difference between both ways?

Update : test results:

var str = '1/1/2000'
var mdy = str.split('/')
console.log( new Date(str) ) // Sat Jan 01 2000 00:00:00 GMT+0500 (Pakistan Standard Time)
console.log( new Date(mdy[2], mdy[0]-1, mdy[1]) ); // Sat Jan 01 2000 00:00:00 GMT+0500 (Pakistan Standard Time)
+4
source share
2 answers

, ( : mdy[0] - 1), new Date(str) ( . §15.9.4.2), ISO 8601 (YYYY-MM-DDTHH:mm:ss.sssZ, . , ):

String [ISO 8601], , , .

( Royi ), RFC 2822 ( MDN), JavaScript, Internet Explorer (. MSDN, - , ).

(MM/DD/YYYY, en-US locale, ). , ( , : , , "" ). :

  • ( <input type="date"/> ), . , ( ) DD/MM/YYYY.
  • , 21 December 2014 ( 21/12/2014 ).
  • ( 21 , , ). , (, 1/2/2014, "" 2nd Jan, 1st Feb). ? new Date(str) , ( , ).

: " ?" , en-US locale (, , , , ), , .

? ( , ), ( moment.js), , , ... .

+1
-1

All Articles