0

How do I get the number of days between two dates in JavaScript?

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

function datediff(first, second) {
    // Take the difference between the dates and divide by milliseconds per day.
    // Round to nearest whole number to deal with DST.
    return Math.round((second-first)/(1000*60*60*24));
}

var first = '1/1/2000';
var second = '1/1/2001';

console.log(datediff(parseDate(first), parseDate(second))); // 366
RUN
add comment
Suggest a different solution or add another example