Last updated: Mar 5, 2024
Reading timeยท14 min
To get the number of months between 2 dates:
getMonth()
method to calculate the month difference between the two
dates.getFullYear()
method to calculate the difference in years between
the dates.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'), ), );
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.
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.
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.
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'), ), );
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.
console.log(Math.abs(-2)); // ๐๏ธ 2 console.log(Math.abs(2)); // ๐๏ธ 2
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.
// โ (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 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
.
To get the number of days between 2 dates:
24 * 60 * 60 * 1000
.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')), );
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.
So, we could have called the getTime()
method on both of the Date
objects to
achieve the same result.
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...
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.
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.
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.
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.
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.
To get the number of hours between 2 dates:
1000 * 60 * 60
.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), ), );
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.
We could have called the getTime()
method on both of the Date
objects to
achieve the same result.
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
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.
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.
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.
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.
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.
To get the number of minutes between 2 dates:
60 * 1000
.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), ), );
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.
We could have called the getTime()
method on both of the Date
objects to
achieve the same result.
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.
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.
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.
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.
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.
To get the number of weeks between 2 dates:
1000 * 60 * 60 * 24 * 7
.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')), );
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.
So, we could have called the getTime()
method on both of the Date
objects to
achieve the same result.
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.
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.
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.
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.
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.
To get the number of seconds between 2 dates:
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), ), );
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.
We could have called the getTime()
method on both of the Date
objects to
achieve the same result.
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), ), );
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.
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.
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.
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.
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.
You can learn more about the related topics by checking out the following tutorials: