Get the Number of Years between 2 Dates in JavaScript

avatar

Borislav Hadzhiev

Mon Jan 17 20222 min read

Get the Number of Years between 2 Dates in JavaScript #

To get the number of year 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')), );

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

The 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.

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 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.

Use the search field on my Home Page to filter through my more than 1,000 articles.