How to Calculate The Number Of Days Between Two Dates in Javascript?


Share:
 | Published By: Admin | Published On: Jan 21, 2022 | Category: Javascript

Hi Dev,

Here, I will show you how to calculate the number of days between two dates in javascript?. This tutorial will give you simple example of count number of days between two dates.

I would like to share with you number of days between two dates javascript. I explained simply about find number of days between two dates javascript calculate.

Let's start following example.

Solution:

<script type="text/javascript">

    function timeDiffCalc(dateFuture, dateNow) {

        let diffInMilliSeconds = Math.abs(dateFuture - dateNow) / 1000;

        // calculate days
        days = Math.floor(diffInMilliSeconds / 86400);
        diffInMilliSeconds -= days * 86400;

        difference = '';

        if (days > 0) {
            difference += (days === 1) ? `${days} Day ` : `${days} Days `;
        }

        return 'Calculate = ' + difference;
    }

    console.log(timeDiffCalc(new Date('2019/10/1 04:10:00'), new Date('2019/10/3 18:20:00')));
  	
</script>

Output:
Calculate = 2 Days
Share: