To get the number of years between 2 dates in JavaScript, we have tested effectively with three methods creating the function with the getTime() method, creating the function with the getFullYears() method, and using the diff() method. Check out the details below to get more information.
Get the number of years between 2 dates in JavaScript
Create the function with the getTime() method
The first way we want to introduce to you to get the number of years is creating the getYearsDiff function using the getTime() method. This method will return the number of milliseconds since January 1, 1970, 00:00:00.
Syntax:
Date.getTime()
Parameter: NONE
Return value: the returns value is a number of milliseconds.
Code example:
In this example, we use the abs function to return a positive number and the round function to return an integer.
function getYearsDiff(day1, day2) {
// Using the getTime() method
var years = (day1.getTime() - day2.getTime()) / 1000;
years /= (60 * 60 * 24);
// Calling round function to return an integer number
return Math.abs(Math.round(years / 365.25));
}
var currentDate = new Date();
var birthDate = new Date("1999-11-11");
console.log("The number of years between currentDate and birthDate is: ",
getYearsDiff(currentDate, birthDate))
Output
The number of years between currentDate and birthDate is: 23
Create the function with the getFullYears() method
Another simple way is creating the function by using the getFullYears() method.
Example:
function getYearsDiff(day1, day2) {
var date1 = new Date(day1);
var date2 = new Date(day2);
// Calling getFullYear() method to get the year of date and subtract them
var years = date2.getFullYear() - date1.getFullYear();
// Calling abs function to return a positive number
return Math.abs(years);
}
var currentDate = new Date();
var birthDate = new Date("1999-11-11");
console.log("The number of years between currentDate and birthDate is: ",
getYearsDiff(currentDate, birthDate))
Output
The number of years between currentDate and birthDate is: 23
Use the diff method
The last way we want to introduce to you to get the number of years between 2 dates in Javascript is using the diff method with the moment library.
Syntax:
data.diff(collection)
Parameter:
- collection: Hold the collection that will be compared with the main collection.
Return value: The return value is a new collection with a difference between collection items.
Example:
import moment from 'moment';
var currentDate = moment([2022, 11, 11]);
var birthDay = moment([1999, 11, 11]);
// Using the diff() function with 'year' to get a different year with 2 dates
var diffTime = currentDate.diff(birthDay, 'year')
console.log("The number of years between 2 dates is: ", diffTime)
Output
The number of years between currentDate and birthDate is: 23
Summary
In this tutorial, we have explained how to get the number of years between 2 dates in JavaScript by using some different methods, but the best simple way and most useful is using the getFullYears() method. We always hope this tutorial is helpful to you. Leave your comment here if you have any questions or comments to improve the article.
Thanks for reading!
Maybe you are interested:
- Get the current year for copyright using javascript
- Add Hours to a Date in JavaScript
- Set the current Date on an Input type=”date” in JavaScript

My name is Fred Hall. My hobby is studying programming languages, which I would like to share with you. Please do not hesitate to contact me if you are having problems learning the computer languages Java, JavaScript, C, C#, Perl, or Python. I will respond to all of your inquiries.
Name of the university: HUSC
Major: IT
Programming Languages: Java, JavaScript, C , C#, Perl, Python