Calculate the Difference between two Dates in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 5, 2024
14 min

banner

# Table of Contents

  1. Get the Number of MONTHS between 2 Dates
  2. Get the Number of YEARS between 2 Dates
  3. Get the Number of DAYS between 2 Dates
  4. Get the Number of HOURS between 2 Dates
  5. Get the Number of MINUTES between 2 Dates
  6. Get the Number of WEEKS between 2 Dates
  7. Get the Number of SECONDS between 2 Dates

# Get the Number of Months between 2 Dates in JavaScript

To get the number of months between 2 dates:

  1. Use the getMonth() method to calculate the month difference between the two dates.
  2. Use the getFullYear() method to calculate the difference in years between the dates.
  3. Multiply the year difference by 12 and return the sum.
index.js
function getMonthDifference(startDate, endDate) { return ( endDate.getMonth() - startDate.getMonth() + 12 * (endDate.getFullYear() - startDate.getFullYear()) ); } // ๐Ÿ‘‡๏ธ 2 console.log( getMonthDifference( new Date('2022-01-15'), new Date('2022-03-16'), ), ); // ๐Ÿ‘‡๏ธ 5 console.log( getMonthDifference( new Date('2022-01-15'), new Date('2022-06-16'), ), ); // ๐Ÿ‘‡๏ธ 14 console.log( getMonthDifference( new Date('2022-01-15'), new Date('2023-03-16'), ), );

get number of months between 2 dates

The code for this article is available on GitHub

We created a reusable function that returns the number of months difference between two dates.

The function takes the start and end Date objects as parameters.

The Date.getMonth() method returns a zero-based value representing the month of the specific Date.

The method returns an integer between 0 and 11, where January is 0, February is 1, etc.

This doesn't matter for our purposes, but it's good to know when working with dates in JavaScript.

We subtracted the month index of the start date from the month index of the end date. This gets us the number of months between the 2 dates (if the 2 dates are in the same year).
index.js
function getMonthDifference(startDate, endDate) { return ( endDate.getMonth() - startDate.getMonth() + 12 * (endDate.getFullYear() - startDate.getFullYear()) ); }

The next step is to account for the possibility that the dates are in different years.

The Date.getFullYear() method returns a 4-digit integer representing the year of the date.

We got the difference in years between the two dates and multiplied by 12 to get the difference in months.

The last step is to add the month difference (from the same year) to the result to get the total number of months between the two dates.

# Table of Contents

  1. Get the Number of YEARS between 2 Dates
  2. Get the Number of DAYS between 2 Dates
  3. Get the Number of HOURS between 2 Dates
  4. Get the Number of MINUTES between 2 Dates
  5. Get the Number of WEEKS between 2 Dates
  6. Get the Number of SECONDS between 2 Dates

# Get the Number of Years between 2 Dates in JavaScript

To get the number of years between 2 dates, use the getFullYear() method to get the year of the date objects and subtract the start date from the end date.

The getFullYear method returns an integer that represents the year of the given date.

index.js
function getYearDiff(date1, date2) { return Math.abs(date2.getFullYear() - date1.getFullYear()); } // ๐Ÿ‘‡๏ธ 0 console.log( getYearDiff(new Date('2022-01-15'), new Date('2022-03-16')), ); // ๐Ÿ‘‡๏ธ 3 console.log( getYearDiff(new Date('2022-01-15'), new Date('2025-06-16')), ); // ๐Ÿ‘‡๏ธ 4 console.log( getYearDiff(new Date('2027-01-15'), new Date('2023-03-16')), ); // โœ… (BIRTHDAYS) Get Year diff considering month function getYearDiffWithMonth(startDate, endDate) { const ms = endDate.getTime() - startDate.getTime(); const date = new Date(ms); return Math.abs(date.getUTCFullYear() - 1970); } // ๐Ÿ‘‡๏ธ 22 console.log( getYearDiffWithMonth( new Date('1999-09-24'), new Date('2022-01-17'), ), ); // ๐Ÿ‘‡๏ธ 7 console.log( getYearDiffWithMonth( new Date('2022-01-15'), new Date('2029-06-16'), ), );

get number of years between 2 dates

The code for this article is available on GitHub

We created a reusable function that takes 2 Date objects and returns the difference in years between them.

The Date.getFullYear() method returns the year of the specified date.

In the first example, we subtract the year of one date from the year of the second date and pass the result to the Math.abs() function.

The Math.abs function is there to make sure that we always return a positive number.

index.js
console.log(Math.abs(-2)); // ๐Ÿ‘‰๏ธ 2 console.log(Math.abs(2)); // ๐Ÿ‘‰๏ธ 2
The function returns the number directly if it is positive, otherwise, it returns the negation of the number.

# Calculating the year difference, considering the month

In the second example, we calculated the year difference considering the month.

For example, if the start date is 1999-09-24 and the end date is 2022-01-17, we would expect to get 22 years back because the end date has not reached the 24th of September yet.

This is useful when calculating birthdays.

index.js
// โœ… (BIRTHDAYS) Get Year diff considering month function getYearDiffWithMonth(startDate, endDate) { const ms = endDate.getTime() - startDate.getTime(); const date = new Date(ms); return Math.abs(date.getUTCFullYear() - 1970); } // ๐Ÿ‘‡๏ธ 22 console.log( getYearDiffWithMonth( new Date('1999-09-24'), new Date('2022-01-17'), ), ); // ๐Ÿ‘‡๏ธ 7 console.log( getYearDiffWithMonth( new Date('2022-01-15'), new Date('2029-06-16'), ), );
The code for this article is available on GitHub

The getTime() method returns the number of milliseconds between January 1st, 1970 and the given date.

By subtracting the timestamp of the start date from the timestamp of the end date, we get back the difference in milliseconds.

We used the difference to create a date object and stored it in the date variable.

The last step is to subtract the year 1970 from the year stored in the date variable and pass the result to Math.abs.

# Table of Contents

  1. Get the Number of DAYS between 2 Dates
  2. Get the Number of HOURS between 2 Dates
  3. Get the Number of MINUTES between 2 Dates
  4. Get the Number of WEEKS between 2 Dates
  5. Get the Number of SECONDS between 2 Dates

# Get the Number of Days between 2 Dates using JavaScript

To get the number of days between 2 dates:

  1. Get the number of milliseconds between the Unix epoch and the Dates.
  2. Subtract the milliseconds of the start date from the milliseconds of the end date.
  3. Divide the result by the number of milliseconds in a day - 24 * 60 * 60 * 1000.
index.js
function getDayDiff(startDate, endDate) { const msInDay = 24 * 60 * 60 * 1000; return Math.round(Math.abs(endDate - startDate) / msInDay); } // ๐Ÿ‘‡๏ธ 10 console.log( getDayDiff(new Date('2021-01-17'), new Date('2021-01-27')), ); // ๐Ÿ‘‡๏ธ 34 console.log( getDayDiff(new Date('2021-01-17'), new Date('2021-02-20')), );

get number of days between 2 dates

The code for this article is available on GitHub

We created a reusable function that returns the number of days between 2 dates.

The msInDay variable stores the number of milliseconds there are in a day.

When you subtract a Date object from another Date object, they both get implicitly converted to a number that represents the milliseconds elapsed between January 1st, 1970 and the given date.

So, we could have called the getTime() method on both of the Date objects to achieve the same result.

index.js
function getDayDiff(startDate, endDate) { const msInDay = 24 * 60 * 60 * 1000; return Math.round( Math.abs(endDate.getTime() - startDate.getTime()) / msInDay ); } // ๐Ÿ‘‡๏ธ 10 console.log( getDayDiff(new Date('2021-01-17'), new Date('2021-01-27')) ); // ๐Ÿ‘‡๏ธ 34 console.log( getDayDiff(new Date('2021-01-17'), new Date('2021-02-20')) ); console.log(new Date().getTime()); // ๐Ÿ‘‰๏ธ 16424539...
The code for this article is available on GitHub

In the example, we called the getTime() method to explicitly convert the Date object to a timestamp when subtracting.

This might be the more clear and explicit approach for readers of your code.

The next step is to divide the result by the number of milliseconds in a day to convert the value from milliseconds to days.

We used the Math.abs() function to handle a scenario where we subtract a greater timestamp from a smaller one.

The Math.abs function returns the absolute value of a number. In other words, if the number is positive, the number is returned and if the number is negative, the negation of the number is returned.

index.js
console.log(Math.abs(-10)); // ๐Ÿ‘‰๏ธ 10 console.log(Math.abs(10)); // ๐Ÿ‘‰๏ธ 10

We passed the value to the Math.round function to round to the nearest integer to deal with Daylight saving time.

Here are some examples of how the Math.round() function works.

index.js
console.log(Math.round(2.49)); // ๐Ÿ‘‰๏ธ 2 console.log(Math.round(2.5)); // ๐Ÿ‘‰๏ธ 3

The function rounds the number up or down to the nearest integer.

If the number is positive and its fractional part is greater than or equal to 0.5, it gets rounded to the next higher absolute value.

If the number is positive and its fractional portion is less than 0.5, it gets rounded to the lower absolute value.

# Table of Contents

  1. Get the Number of HOURS between 2 Dates
  2. Get the Number of MINUTES between 2 Dates
  3. Get the Number of WEEKS between 2 Dates
  4. Get the Number of SECONDS between 2 Dates

# Get the Number of Hours between 2 Dates in JavaScript

To get the number of hours between 2 dates:

  1. Get the number of milliseconds between the Unix epoch and the Dates.
  2. Subtract the milliseconds of the start date from the milliseconds of the end date.
  3. Divide the result by the number of milliseconds in an hour - 1000 * 60 * 60.
index.js
function getHoursDiff(startDate, endDate) { const msInHour = 1000 * 60 * 60; return Math.round(Math.abs(endDate - startDate) / msInHour); } // ๐Ÿ‘‡๏ธ (9 hours - 6 hours = 3 hours) console.log( getHoursDiff( new Date(2022, 1, 24, 6, 44, 0), new Date(2022, 1, 24, 9, 45, 30), ), ); // ๐Ÿ‘‡๏ธ (1 day - 0 hours = 24 hours) console.log( getHoursDiff( new Date(2022, 1, 23, 9, 40, 0), new Date(2022, 1, 24, 9, 45, 0), ), );

get number of hours between 2 dates

The code for this article is available on GitHub

The parameters we passed to the Date() constructor in the examples are - year, month (0 = January, 1 = February, etc), day of the month, hour, minutes and seconds.

We created a reusable function that returns the number of hours between 2 dates.

The msInHour variable stores the number of milliseconds there are in an hour.

When you subtract a Date object from another Date object, they both get implicitly converted to a number that represents the milliseconds elapsed between January 1st, 1970 and the given date.

# Get the Number of Hours between 2 Dates using getTime()

We could have called the getTime() method on both of the Date objects to achieve the same result.

index.js
function getHoursDiff(startDate, endDate) { const msInHour = 1000 * 60 * 60; return Math.round( Math.abs(endDate.getTime() - startDate.getTime()) / msInHour, ); } // ๐Ÿ‘‡๏ธ (9 hours - 6 hours = 3 hours) console.log( getHoursDiff( new Date(2022, 1, 24, 6, 44, 0), new Date(2022, 1, 24, 9, 45, 30), ), ); // ๐Ÿ‘‡๏ธ (1 day - 0 hours = 24 hours) console.log( getHoursDiff( new Date(2022, 1, 23, 9, 40, 0), new Date(2022, 1, 24, 9, 45, 0), ), ); console.log(new Date().getTime()); // ๐Ÿ‘‰๏ธ 164243142779
The code for this article is available on GitHub

In the example, we called the getTime() method to explicitly convert the Date object to timestamp when subtracting.

This might be the more clear and explicit approach for readers of your code.

The next step is to divide the result by the number of milliseconds in an hour to convert the value from milliseconds to hours.

We used the Math.abs() function to handle a scenario where we subtract a greater timestamp from a smaller one.

The Math.abs function returns the absolute value of a number. In other words, if the number is positive, the number is returned and if the number is negative, the negation of the number is returned.

index.js
console.log(Math.abs(-3)); // ๐Ÿ‘‰๏ธ 3 console.log(Math.abs(3)); // ๐Ÿ‘‰๏ธ 3

We passed the value to the Math.round function to round to the nearest integer.

Here are some examples of how the Math.round function works.

index.js
console.log(Math.round(2.49)); // ๐Ÿ‘‰๏ธ 2 console.log(Math.round(2.5)); // ๐Ÿ‘‰๏ธ 3

The function rounds the number up or down to the nearest integer.

If the number is positive and its fractional part is greater than or equal to 0.5, it gets rounded to the next higher absolute value.

If the number is positive and its fractional portion is less than 0.5, it gets rounded to the lower absolute value.

# Table of Contents

  1. Get the Number of MINUTES between 2 Dates
  2. Get the Number of WEEKS between 2 Dates
  3. Get the Number of SECONDS between 2 Dates

# Get the Number of Minutes between 2 Dates using JavaScript

To get the number of minutes between 2 dates:

  1. Get the number of milliseconds between the Unix epoch and the Dates.
  2. Subtract the milliseconds of the start date from the milliseconds of the end date.
  3. Divide the result by the number of milliseconds in a minute - 60 * 1000.
index.js
function getMinDiff(startDate, endDate) { const msInMinute = 60 * 1000; return Math.round(Math.abs(endDate - startDate) / msInMinute); } // ๐Ÿ‘‡๏ธ (45 minutes - 30 minutes = 15 minutes) console.log( getMinDiff( new Date(2022, 1, 24, 9, 30, 0), new Date(2022, 1, 24, 9, 45, 0), ), ); // ๐Ÿ‘‡๏ธ (1hr 45mins - 30 mins = 75 mins) console.log( getMinDiff( new Date(2022, 1, 24, 9, 30, 0), new Date(2022, 1, 24, 10, 45, 0), ), );

get number of minutes between two dates

The code for this article is available on GitHub

The parameters we passed to the Date() constructor in the examples are - year, month (0 = January, 1 = February, etc), day of the month, hour, minutes and seconds.

We created a reusable function that returns the difference in minutes between 2 dates.

The msInMinute variable stores the number of milliseconds there are in a minute.

When you subtract a Date object from another Date object, they both get implicitly converted to a number that represents the milliseconds elapsed between January 1st, 1970 and the given date.

# Get the Number of Minutes between 2 Dates using getTime()

We could have called the getTime() method on both of the Date objects to achieve the same result.

index.js
function getMinDiff(startDate, endDate) { const msInMinute = 60 * 1000; return Math.round( Math.abs(endDate.getTime() - startDate.getTime()) / msInMinute, ); } // ๐Ÿ‘‡๏ธ (45 minutes - 30 minutes = 15 minutes) console.log( getMinDiff( new Date(2022, 1, 24, 9, 30, 0), new Date(2022, 1, 24, 9, 45, 0), ), ); // ๐Ÿ‘‡๏ธ (1hr 45mins - 30 mins = 75 mins) console.log( getMinDiff( new Date(2022, 1, 24, 9, 30, 0), new Date(2022, 1, 24, 10, 45, 0), ), ); console.log(new Date().getTime()); // ๐Ÿ‘‰๏ธ 1642427316049

In the example, we called the getTime() method to explicitly convert the Date object to timestamp when subtracting.

This might be the more clear and explicit approach for readers of your code.

The next step is to divide the result by the number of milliseconds in a minute to convert the value from milliseconds to minutes.

We used the Math.abs() function to handle a scenario where we subtract a greater timestamp from a smaller one.

The Math.abs function returns the absolute value of a number. In other words, if the number is positive, the number is returned and if the number is negative, the negation of the number is returned.

index.js
console.log(Math.abs(-5)); // ๐Ÿ‘‰๏ธ 5 console.log(Math.abs(5)); // ๐Ÿ‘‰๏ธ 5

We passed the value to the Math.round function to round to the nearest integer.

Here are some examples of how the Math.round() function works.

index.js
console.log(Math.round(5.49)); // ๐Ÿ‘‰๏ธ 5 console.log(Math.round(5.5)); // ๐Ÿ‘‰๏ธ 6

The function rounds the number up or down to the nearest integer.

If the number is positive and its fractional part is greater than or equal to 0.5, it gets rounded to the next higher absolute value.

If the number is positive and its fractional portion is less than 0.5, it gets rounded to the lower absolute value.

# Table of Contents

  1. Get the Number of WEEKS between 2 Dates
  2. Get the Number of SECONDS between 2 Dates

# Get the Number of Weeks between 2 Dates using JavaScript

To get the number of weeks between 2 dates:

  1. Get the number of milliseconds between the Unix epoch and the Dates.
  2. Subtract the milliseconds of the start date from the milliseconds of the end date.
  3. Divide the result by the number of milliseconds in a week - 1000 * 60 * 60 * 24 * 7.
index.js
function getWeeksDiff(startDate, endDate) { const msInWeek = 1000 * 60 * 60 * 24 * 7; return Math.round(Math.abs(endDate - startDate) / msInWeek); } // ๐Ÿ‘‡๏ธ 3 weeks console.log( getWeeksDiff(new Date('2022-01-01'), new Date('2022-01-24')), ); // ๐Ÿ‘‡๏ธ 4 weeks console.log( getWeeksDiff(new Date('2022-02-01'), new Date('2022-03-01')), );

get number of weeks between 2 dates

The code for this article is available on GitHub

We created a reusable function that returns the number of weeks between 2 dates.

The msInWeek variable stores the number of milliseconds there are in a week.

When you subtract a Date object from another Date object, they both get implicitly converted to a number that represents the milliseconds elapsed between January 1st, 1970 and the given date.

# Get the Number of Weeks between 2 Dates using getTime()

So, we could have called the getTime() method on both of the Date objects to achieve the same result.

index.js
function getWeeksDiff(startDate, endDate) { const msInWeek = 1000 * 60 * 60 * 24 * 7; return Math.round( Math.abs(endDate.getTime() - startDate.getTime()) / msInWeek, ); } // ๐Ÿ‘‡๏ธ 3 weeks console.log( getWeeksDiff( new Date('2022-01-01'), new Date('2022-01-24') ) ); // ๐Ÿ‘‡๏ธ 4 weeks console.log( getWeeksDiff( new Date('2022-02-01'), new Date('2022-03-01') ) ); console.log(new Date().getTime()); // ๐Ÿ‘‰๏ธ 1642432565954

In the example, we called the getTime() method to explicitly convert the Date object to timestamp when subtracting.

This might be the more clear and explicit approach for readers of your code.

The next step is to divide the result by the number of milliseconds in a week to convert the value from milliseconds to weeks.

We used the Math.abs() function to handle a scenario where we subtract a greater timestamp from a smaller one.

The Math.abs function returns the absolute value of a number. In other words, if the number is positive, the number is returned and if the number is negative, the negation of the number is returned.

index.js
console.log(Math.abs(-3)); // ๐Ÿ‘‰๏ธ 3 console.log(Math.abs(3)); // ๐Ÿ‘‰๏ธ 3

We passed the value to the Math.round function to round to the nearest integer.

Here are some examples of how the Math.round function works.

index.js
console.log(Math.round(7.49)); // ๐Ÿ‘‰๏ธ 7 console.log(Math.round(7.5)); // ๐Ÿ‘‰๏ธ 8

The function rounds the number up or down to the nearest integer.

If the number is positive and its fractional part is greater than or equal to 0.5, it gets rounded to the next higher absolute value.

If the number is positive and its fractional portion is less than 0.5, it gets rounded to the lower absolute value.

# Get the Number of Seconds between 2 Dates in JavaScript

To get the number of seconds between 2 dates:

  1. Get the number of milliseconds between the Unix epoch and the Dates.
  2. Subtract the milliseconds of the start date from the milliseconds of the end date.
  3. Divide the result by the number of milliseconds in a second (1000).
index.js
function getSecondsDiff(startDate, endDate) { const msInSecond = 1000; return Math.round( Math.abs(endDate - startDate) / msInSecond ); } // ๐Ÿ‘‡๏ธ (1 min 30 seconds - 0 seconds = 90 seconds) console.log( getSecondsDiff( new Date(2022, 1, 24, 9, 44, 0), new Date(2022, 1, 24, 9, 45, 30), ), ); // ๐Ÿ‘‡๏ธ (5 minutes - 0 seconds = 300 seconds) console.log( getSecondsDiff( new Date(2022, 1, 24, 10, 40, 0), new Date(2022, 1, 24, 10, 45, 0), ), );

get number of seconds between 2 dates

The code for this article is available on GitHub

The parameters we passed to the Date() constructor in the examples are - year, month (0 = January, 1 = February, etc), day of the month, hour, minutes and seconds.

We created a reusable function that returns the number of seconds between 2 dates.

The msInSecond variable stores the number of milliseconds there are in a second.

When you subtract a Date object from another Date object, they both get Implicitly converted to a number that represents the milliseconds elapsed between January 1st, 1970 and the given date.

# Get the Number of Seconds between 2 Dates using getTime

We could have called the getTime() method on both of the Date objects to achieve the same result.

index.js
function getSecondsDiff(startDate, endDate) { const msInSecond = 1000; return Math.round( Math.abs(endDate.getTime() - startDate.getTime()) / msInSecond, ); } // ๐Ÿ‘‡๏ธ (1 min 30 seconds - 0 seconds = 90 seconds) console.log( getSecondsDiff( new Date(2022, 1, 24, 9, 44, 0), new Date(2022, 1, 24, 9, 45, 30), ), ); // ๐Ÿ‘‡๏ธ (5 minutes - 0 seconds = 300 seconds) console.log( getSecondsDiff( new Date(2022, 1, 24, 10, 40, 0), new Date(2022, 1, 24, 10, 45, 0), ), );
The code for this article is available on GitHub

This time we called the getTime() method to explicitly convert the Date object to a timestamp when subtracting.

This might be the more clear and explicit approach for readers of your code.

The next step is to divide the result by the number of milliseconds in a second to convert the value from milliseconds to seconds.

We used the Math.abs() function to handle a scenario where we subtract a greater timestamp from a smaller one.

The Math.abs function returns the absolute value of a number. In other words, if the number is positive, the number is returned and if the number is negative, the negation of the number is returned.

index.js
console.log(Math.abs(-100)); // ๐Ÿ‘‰๏ธ 100 console.log(Math.abs(100)); // ๐Ÿ‘‰๏ธ 100

We passed the value to the Math.round function to round to the nearest integer.

Here are some examples of how the Math.round function works.

index.js
console.log(Math.round(1.49)); // ๐Ÿ‘‰๏ธ 1 console.log(Math.round(1.5)); // ๐Ÿ‘‰๏ธ 2

The function rounds the number up or down to the nearest integer.

If the number is positive and its fractional part is greater than or equal to 0.5, it gets rounded to the next higher absolute value.

If the number is positive and its fractional portion is less than 0.5, it gets rounded to the lower absolute value.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev