Navigate back to the homepage

How to get months between two dates in javascript?

Infinitbility
JavaScript
June 9th, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To get months between two dates,

  1. Use the getMonth() method to get the months of both dates, and subtract them to get the difference of months
  2. Use the getFullYear() method to get the years of both dates, and subtract them to get the difference of years
  3. Multiply the year difference by 12 and return the sum.

It will return your difference of dates in months.

The getMonth() method returns the month on the specified date according to local time, as a zero-based value (where zero indicates the first month of the year).

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

Today, I’m going to show How do I get months between two dates in javascript, here I will use the javascript getMonth() method and getFullYear() method to get difference numbers of months.

Let’s start the today’s tutorial How do you get months 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 months
  3. Use a function for multiple date examples

let’s write the code.

1function getMonths(startDate, endDate) {
2 return (endDate.getMonth() - startDate.getMonth() + 12 * (endDate.getFullYear() - startDate.getFullYear()));
3}
4
5// 👇️ 2
6console.log(getMonths(new Date('2022-01-15'), new Date('2022-03-16')));
7
8// 👇️ 5
9console.log(getMonths(new Date('2022-01-15'), new Date('2022-06-16')));
10
11// 👇️ 14
12console.log(getMonths(new Date('2022-01-15'), new Date('2023-03-16')));

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

let’s check the output.

javascript, get months between two dates example
javascript, get months 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 year difference between two dates in javascript?

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

June 9th, 2022 · 1 min read

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
© 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]