Skip to content

Instantly share code, notes, and snippets.

@jacaetevha
Created February 27, 2009 04:39
Show Gist options
  • Save jacaetevha/71291 to your computer and use it in GitHub Desktop.
Save jacaetevha/71291 to your computer and use it in GitHub Desktop.
function monthsBetween(thisDate, thatDate) {
if (thisDate > thatDate) {
return monthsBetween(thatDate, thisDate);
}
var number = 0;
if (thatDate.getFullYear() > thisDate.getFullYear()) {
number = number + (thatDate.getFullYear() - thisDate.getFullYear() - 1) * 12;
} else {
return thatDate.getMonth() - thisDate.getMonth();
}
if (thatDate.getMonth() > thisDate.getMonth()) {
number = number + 12 + thatDate.getMonth() - thisDate.getMonth();
} else {
number = number + (12 - thisDate.getMonth()) + thatDate.getMonth();
}
return number;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment