Let’s do this again. Say you have these two dates:
1 June 2020
1 September 2019
How many months do you have between these dates? Here, we can calculate the number of months by adding 12 (the number of months in a year) to 6 (for June). This totals 18. Then, we use 18 minus 9 (for September). And we know there are 9 months between 1 June 2020 and 1 September 2020.
This time, we need to add 24 (2 years worth) to 6. This adds up to 30. Then, we subtract 30 by 9 (for September). And we know there are 21 months between 1 June 2021 and 1 September 2019.
One more time. Let’s say you have these two dates. What’s the number of months between them?
1 April 2019
11 March 2019
The answer is zero.
There are zero months between the 11 March 2019 and 1 April 2019. But there are 21 days between the two dates.
If there’s one full month between your two dates (like 1 Jan to 1 Feb, or 15 Feb to 15 March), you count the month as one month. If there’s no full month, you don’t count the month. You count the number of days in the month instead.
The easiest way to check for full months is:
Create a throwaway date with the date1.
Subtract monthDiff from the throwaway date.
Check if the throwaway date is later compared to date2.
If the throwaway date is larger, there’s a full month.
Otherwise, there are no full months.
const date1 = new Date(2019, 1, 5)
const date2 = new Date(2019, 0, 3)
let monthDiff = date1.getMonth() - date2.getMonth()
// Check if there's a full month of difference
const d = new Date(date1)
d.setMonth(date1.getMonth() - 1)
if (d < date2) monthDiff = monthDiff - 1
Let’s put what we know into a function.
const getMonthDiff = (endDate, startDate) => {
const yearDiff = endDate.getFullYear() - startDate.getFullYear()
let monthDiff = endDate.getMonth() + yearDiff * 12 - startDate.getMonth()
// Check if there's a full month of difference
const d = new Date(endDate)
d.setMonth(endDate.getMonth() - monthDiff)
if (d < startDate) monthDiff = monthDiff - 1
return monthDiff
}
Calculating Months and Days
Let’s say you have these two dates:
5 February 2019
3 January 2019
We know there are 1 month and 2 days between the dates. The timestamp between these two dates is: 2851200000
To calculate the difference in days, we need to find the remaining difference in milliseconds. To find the remaining difference in milliseconds, we need to subtract the original difference by the number of days in the month.
In this case, there are 31 days from 3 January to 3 February.
Then, to get the number of days, we divide the remaining difference by the number of milliseconds in a day.
const days = Math.floor(remainingDiff / toMilliseconds('days'))
console.log(days) // 2
Let’s pause for a moment here because we used a magic number as we calculated remainingDiff. We used 31. How do we get 31 programmatically?
Getting the number of days in a month
First, we know there are 31 days in January. This means there are 31 days from 1 January to 1 February. It also means there are 31 days from 3 January to 3 February.
To get 31, we get the number of days in January, like this:
To get the remaining difference, we need to calculate the number of days that have passed during these 9 months.
September 2019: 30 days
October 2019: 31 days
November 2019: 30 days
December 2019: 31 days
January 2019: 31 days
February 2019: 29 days
March 2019: 31 days
April 2019: 30 days
May 2019: 31 days
So there are 274 days between 1 September 2019 and 1 June 2020.
In code, we can create a loop with 9 months of differences. You can use a for loop if you want to, but I’m going to use Array.from to create an array, and map to loop through it.
It makes sense for us to calculate this difference (for days and milliseconds passed for Month calculations) directly in getMonthDiff. This makes the remaining calculation much easier.
Now, let’s say you want to countdown to the end of this year. To count to the end of this year, you can increase the year value by 1, set month to 0, and day to 1.
const now = new Date()
const nextYear = new Date(now.getFullYear() + 1, 0, 1)