getDaysBetweenDates()

Last updated: June 30, 2021

Calculates the difference (in days) between two dates.

Description

Subtract the two Date objects and divide by the number of milliseconds in a day to get the difference (in days) between them.

const getDaysBetweenDates = (startDate, endDate) =>
    (endDate - startDate) / (1000 * 60 * 60 * 24);

To convert in milliseconds: take 1000 milliseconds and then multiply it by 60 seconds, then multiply by 60 minutes, then multiply by 24 hours.

Parameters

startDate - the starting date in yyyy/MM/dd date format

endDate - the ending date in yyyy/MM/dd date format

Return Values

Returns a number of days

Usage

You can pass the start and end date in its argument by using JavaScript's native Date Object.

getDaysBetweenDates(new Date('2021/06/27'), new Date('2021/07/20')); // 23

Live Example

From:
To:

Hey, did you find this shortcode useful? I would appreciate if you could share this to your networks.