Navigate back to the homepage

how to get year difference between two dates in javascript?

Infinitbility
JavaScript
June 9th, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To get years between two dates,

  1. Extract both dates and years using the getFullYear() method.
  2. subtract them to get a number of years between dates.

It will return your difference in dates in a number of years.

The getFullYear() method returns the year of the specified date according to local time.

Today, I’m going to show How do I get years between two dates in javascript, here I will use the javascript getFullYear() method and the above-mentioned conditions to get years between the start and end date time.

Let’s start the today’s tutorial How do you get years between two dates in javascript?

In the following example, we are going to do

  1. create two dates starting and ending date
  2. create a function that accepts a date and returns diff years
  3. Use a function for multiple date examples

let’s write the code.

1function getYears(startingDate, endingDate) {
2 return endingDate.getFullYear() - startingDate.getFullYear();
3}
4
5// 👇️ 10
6console.log(getYears(new Date('2012-01-15'), new Date('2022-03-16')));
7
8// 👇️ 8
9console.log(getYears(new Date('2014-01-15'), new Date('2022-06-16')));
10
11// 👇️ 8
12console.log(getYears(new Date('2015-01-15'), new Date('2023-03-16')));

In the above program, we have created a custom function getYears() and pass dates to get a mumber of years.

let’s check the output.

javascript, get years between two dates example
javascript, get years between two dates example

I hope it’s help you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to get days between two dates in javascript?

Example of get days between two dates in javascript with step by step guide.

June 8th, 2022 · 1 min read

How to get hours between two dates in javascript?

Example of get hours between two dates in javascript with step by step guide.

June 8th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]