But how many years are there between these two dates?
1 January 2020
1 February 2019
You guessed it. There are zero years and 11 months between 1 January 2020 and 1 February 2019. Like how we checked for full months when we count months, we need to check for full years when we count years.
let yearDiff = endDate.getFullYear() - startDate.getFullYear()
// Check if there's full years of difference
const d = new Date(endDate)
d.setFullYear(endDate.getFullYear() - yearDiff)
if (d < startDate) yearDiff = yearDiff - 1
Letās put what we know into a function first.
const getYearDiff = (endDate, startDate) => {
let yearDiff = endDate.getFullYear() - startDate.getFullYear()
// Check if there's full years of difference
const d = new Date(endDate)
d.setFullYear(endDate.getFullYear() - yearDiff)
if (d < startDate) yearDiff = yearDiff - 1
return yearDiff
}
Leap years
There are 365 days in a year, but not when a leap year occurs. When thereās a leap year, there are 366 days in the year. We need to get the right number of days in the year to get a correct countdown.
Checking for a leap year
When thereās a leap year, thereās a February 29 in the year. We can use this information to check if itās a leap year.
This is where it gets complicated. I want to bring you through an exercise to build your problem-solving skills. So follow along.
How many days are there from:
1 Feb 2019 to 1 Feb 2020?
1 March 2019 to 1 March 2020?
1 Feb 2020 to 1 Feb 2021?
1 March 2020 to 1 March 2021?
Hereās the answers:
Feb 2019 to Feb 2020: 365 days. (Feb 2019 has 28 days)
March 2019 to March 2020: 366 days. (Feb 2020 has 29 days)
Feb 2020 to Feb 2021: 366 days. (Feb 2020 has 29 days)
March 2020 to March 2021: 365 days. (Feb 2021 has 28 days)
There are either 365 days or 366 days in a year, but the number of days depend on the year and month we count from. Itās easier to see a pattern if we reorder the above into this:
365 days:
Feb 2019 to Feb 2020
March 2020 to March 2021
366 days:
March 2019 to March 2020
Feb 2020 to Feb 2021
We can bold the leap year to make the information easier to digest.
365 days:
Feb 2019 to Feb 2020
March 2020 to March 2021
366 days:
March 2019 to March 2020
Feb 2020 to Feb 2021
Letās expand this a bit more.
365 days:
Jan 2019 to Jan 2020
Feb 2019 to Feb 2020
March 2020 to March 2021
April 2020 to April 2021
366 days:
March 2019 to March 2020
April 2019 to April 2020
Jan 2020 to Jan 2021
Feb 2020 to Feb 2021
Can you spot a pattern?
365 days:
Jan 2019 to Jan 2020 (2019 has no Feb 29)
Feb 2019 to Feb 2020 (2019 has no Feb 29)
March 2020 to March 2021 (2021 has no Feb 29)
April 2020 to April 2021 (2021 has no Feb 29)
366 days:
March 2019 to March 2020 (2020 has Feb 29)
April 2019 to April 2020 (2020 has Feb 29)
Jan 2020 to Jan 2021 (2020 has Feb 29)
Feb 2020 to Feb 2021 (2020 has Feb 29)
Hereās the pattern:
If we count from March onwards, we need to check for a Feb 29 in the next year
If we count from Jan or Feb of the year, we need to check for Feb 29 this year
To get the the difference in months, we need to adjust date values passed into getMonthDiff. Here, we need to increase startDate by the number of years. (If you subtracted yearDiff from endDate, youāll get a wrong value. See if you can figure out why š).