OneCompiler

How can i calculate the number of days between 2 dates in JavaScript?

I need to populate the number of days between 2 dates in my application. Can anyone suggest best solution to do it in JavaScript?

1 Answer

6 years ago by

I usually prefer moment.js as it is easy to use. check below.

    let startDate = moment('01/01/2020','MM/DD/YYYY');
    let endDate = moment('01/10/2020','MM/DD/YYYY');
    let diffDays = endDate.diff(startDate, 'days');
    console.log(diffDays); 
6 years ago by Divya