… of many things …

… of many things … header image 2

Javascript: Get number of months between two dates

April 2nd, 2007 · No Comments

JAVASCRIPT:
  1. function monthsBetween(thisDate, thatDate) {
  2. if (thisDate> thatDate) {
  3. return monthsBetween(thatDate, thisDate);
  4. }
  5.  
  6. var number = 0;
  7. if (thatDate.getFullYear()> thisDate.getFullYear()) {
  8. number = number + (thatDate.getFullYear() - thisDate.getFullYear() - 1) * 12;
  9. } else {
  10. return thatDate.getMonth() - thisDate.getMonth();
  11. }
  12.  
  13. if (thatDate.getMonth()> thisDate.getMonth()) {
  14. number = number + 12 + thatDate.getMonth() - thisDate.getMonth();
  15. } else {
  16. number = number + (12 - thisDate.getMonth()) + thatDate.getMonth();
  17. }
  18. return number;
  19. }

Tags: Computing · Software

0 responses so far ↓

  • There are no comments yet...Kick things off by filling out the form below.

Leave a Comment