Count whole months between two dates in JavaScript
Intermediate · 4 min read
"Member for 7 months", "3 months left on your contract", "the baby is 14 months old": counting whole months between two dates comes up often. Days are easy to count because every day has the same length. Months are not, so you can't just divide by 30.
In this tutorial you will write monthsBetween(from, to). It counts complete months. 15 January to 15 March is two months, and 15 January to 14 March is only one, because the second month isn't finished. You will also make the answer turn negative cleanly when the dates are the other way round. You need to know how to make a date with new Date(year, month, day) and that months count from 0.
Count on the calendar, not in milliseconds
Months have 28, 29, 30 or 31 days, so there is no fixed number of milliseconds in a month. Instead, count the way you would on a paper calendar: how many times the month number changed, then check whether the day of the month has come round yet.
To turn a year and a month into one running count, multiply the year by 12 and add the month. Then the difference between two dates is:
const months = (2027 - 2026) * 12 + (0 - 0);
console.log(months); // 12
That is January 2026 to January 2027: 12 months.
Here is the exercise. If you want to try it first, go ahead. Otherwise read on and we will build it together.
28 Whole months between two dates
fairCount complete months from one date to another: 15 January to 14 March is one month, 15 January to 15 March is two.
Stuck? Three hints, smallest first
- 1. Years times twelve, plus the difference in months, is the starting figure.
- 2. Then take one off if the day of the month has not been reached yet.
- 3. Handle the reversed case by recursing with the arguments swapped, so the answer is symmetric.
Step 1: the month difference
Start with years times twelve, plus the difference in months:
const jan15 = new Date(2026, 0, 15);
const mar14 = new Date(2026, 2, 14);
let months = (mar14.getFullYear() - jan15.getFullYear()) * 12 + (mar14.getMonth() - jan15.getMonth());
console.log(months); // 2
Month 2 is March, so the difference is 2. But 15 January to 14 March is one day short of two full months. This starting figure counts month changes, not complete months.
Step 2: has the day come round?
A month is only complete once the day of the month reaches the starting day again. If to's day is smaller than from's day, the last month isn't finished, so take one off:
function monthsBetween(from, to) {
let months = (to.getFullYear() - from.getFullYear()) * 12 + (to.getMonth() - from.getMonth());
if (to.getDate() < from.getDate()) months--; // the day of the month has not come round
return months;
}
console.log(monthsBetween(jan15, new Date(2026, 2, 14))); // 1
console.log(monthsBetween(jan15, new Date(2026, 2, 15))); // 2
console.log(monthsBetween(jan15, new Date(2027, 0, 15))); // 12
console.log(monthsBetween(jan15, jan15)); // 0
On 14 March, 14 is less than 15, so the count drops to 1. On 15 March the day has come round, so it is 2.
Step 3: the wrong turn with reversed dates
What if to is before from? Some cases look fine:
console.log(monthsBetween(new Date(2026, 2, 15), jan15)); // -2
console.log(monthsBetween(new Date(2026, 2, 20), jan15)); // -3
console.log(monthsBetween(jan15, new Date(2026, 2, 20))); // 2
The last two lines are the same pair of dates. Forwards, the answer is 2. Backwards, it is -3, not -2. The day check was written for going forwards: it takes one off when to's day is smaller, and that pushes a negative answer further from zero.
Step 4: make it symmetric
The simplest fix: if the dates are backwards, swap them, count forwards, and flip the sign. The function can call itself to do it:
function monthsBetween(from, to) {
if (to < from) return -monthsBetween(to, from); // symmetric, so the sign is honest
let months = (to.getFullYear() - from.getFullYear()) * 12 + (to.getMonth() - from.getMonth());
if (to.getDate() < from.getDate()) months--; // the day of the month has not come round
return months;
}
console.log(monthsBetween(new Date(2026, 2, 20), jan15)); // -2
to < from works on dates because < compares their timestamps. Now the backwards answer is always the forwards answer with a minus sign.
Step 5: the month-end case
What about 31 January to 28 February?
console.log(monthsBetween(new Date(2026, 0, 31), new Date(2026, 1, 28))); // 0
console.log(monthsBetween(new Date(2026, 0, 31), new Date(2026, 2, 3))); // 1
console.log(monthsBetween(new Date(2026, 10, 30), new Date(2026, 11, 31))); // 1
Many people would call 31 January to 28 February a whole month, since February has no later day. This rule says 0, because 28 is less than 31. That is a choice, not a bug, and date libraries don't all agree. If you need the other reading, add a rule: count the month as complete when to is the last day of its month. Either way, write the choice down and test it.
The full solution
The panel has the symmetric monthsBetween, then checks full months, one day short, a whole year, the same day, reversed dates and the month-end cases. Press Run to see the output.
function monthsBetween(from, to) {
if (to < from) return -monthsBetween(to, from); // symmetric, so the sign is honest
let months = (to.getFullYear() - from.getFullYear()) * 12 + (to.getMonth() - from.getMonth());
if (to.getDate() < from.getDate()) months--; // the day of the month has not come round
return months;
}
const jan15 = new Date(2026, 0, 15);
console.log('15/01 to 15/03:', monthsBetween(jan15, new Date(2026, 2, 15)));
console.log('15/01 to 14/03:', monthsBetween(jan15, new Date(2026, 2, 14)), '<- a day short');
console.log('31/01 to 28/02:', monthsBetween(new Date(2026, 0, 31), new Date(2026, 1, 28)), '<- debatable');
Output
8 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 8 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('two full months', monthsBetween(jan15, new Date(2026, 2, 15)), 2);
test('one day short of two', monthsBetween(jan15, new Date(2026, 2, 14)), 1);
test('a year is twelve', monthsBetween(jan15, new Date(2027, 0, 15)), 12);
test('the same day is zero', monthsBetween(jan15, jan15), 0);
test('reversing the arguments negates it', monthsBetween(new Date(2026, 2, 15), jan15), -2);
test('31 January to 28 February counts as none', monthsBetween(new Date(2026, 0, 31), new Date(2026, 1, 28)), 0);
test('but 31 January to 3 March counts as one', monthsBetween(new Date(2026, 0, 31), new Date(2026, 2, 3)), 1);
test('30 November to 31 December is one', monthsBetween(new Date(2026, 10, 30), new Date(2026, 11, 31)), 1);
The month difference is easy; the correction for "the day has not come round yet" is the part that gets forgotten, and without it 15 January to 1 February reports a whole month.
The sixth assertion is genuinely arguable, and worth being explicit about rather than hiding. 31 January to 28 February is a whole calendar month by most people's reading, but this rule says zero, because 28 is less than 31. Libraries differ. If you need the other reading, add a clause: count it as a whole month when to is the last day of its month. Pick one, write it down, and test it — the disagreement only becomes a bug when it is undocumented.
Common mistakes
- Leaving out the day check. Without it, 15 January to 1 February counts as a whole month.
- Dividing the days by 30. Months aren't all 30 days long, so the answer drifts across a year.
- Not testing reversed dates. The simple version gives -3 where -2 is right.
Try it yourself
- How many whole months are there from 1 January 2026 to 31 December 2026?
- Add the "last day of the month" rule from Step 5 so 31 January to 28 February counts as 1.
- Write
monthsLeft(today, end)for a contract and return 0 instead of a negative number once it has ended.