How To Get The Number Of Years Between 2 Dates In JavaScript

Get the Number of Years between 2 Dates in JavaScript

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:

Leave a Reply

Your email address will not be published. Required fields are marked *