Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 695101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:55:29+00:00 2026-05-14T02:55:29+00:00

How would I work out the difference for two Date() objects in JavaScript, while

  • 0

How would I work out the difference for two Date() objects in JavaScript, while only return the number of months in the difference?

Any help would be great πŸ™‚

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:55:29+00:00Added an answer on May 14, 2026 at 2:55 am

    The definition of “the number of months in the difference” is subject to a lot of interpretation. πŸ™‚

    You can get the year, month, and day of month from a JavaScript date object. Depending on what information you’re looking for, you can use those to figure out how many months are between two points in time.

    For instance, off-the-cuff:

    function monthDiff(d1, d2) {
        var months;
        months = (d2.getFullYear() - d1.getFullYear()) * 12;
        months -= d1.getMonth();
        months += d2.getMonth();
        return months <= 0 ? 0 : months;
    }
    
    function monthDiff(d1, d2) {
        var months;
        months = (d2.getFullYear() - d1.getFullYear()) * 12;
        months -= d1.getMonth();
        months += d2.getMonth();
        return months <= 0 ? 0 : months;
    }
    
    function test(d1, d2) {
        var diff = monthDiff(d1, d2);
        console.log(
            d1.toISOString().substring(0, 10),
            "to",
            d2.toISOString().substring(0, 10),
            ":",
            diff
        );
    }
    
    test(
        new Date(2008, 10, 4), // November 4th, 2008
        new Date(2010, 2, 12)  // March 12th, 2010
    );
    // Result: 16
    
    test(
        new Date(2010, 0, 1),  // January 1st, 2010
        new Date(2010, 2, 12)  // March 12th, 2010
    );
    // Result: 2
    
    test(
        new Date(2010, 1, 1),  // February 1st, 2010
        new Date(2010, 2, 12)  // March 12th, 2010
    );
    // Result: 1

    (Note that month values in JavaScript start with 0 = January.)

    Including fractional months in the above is much more complicated, because three days in a typical February is a larger fraction of that month (~10.714%) than three days in August (~9.677%), and of course even February is a moving target depending on whether it’s a leap year.

    There are also some date and time libraries available for JavaScript that probably make this sort of thing easier.


    Note: There used to be a + 1 in the above, here:

    months = (d2.getFullYear() - d1.getFullYear()) * 12;
    months -= d1.getMonth() + 1;
    // βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’^^^^
    months += d2.getMonth();
    

    That’s because originally I said:

    …this finds out how many full months lie between two dates, not counting partial months (e.g., excluding the month each date is in).

    I’ve removed it for two reasons:

    1. Not counting partial months turns out not to be what many (most?) people coming to the answer want, so I thought I should separate them out.

    2. It didn’t always work even by that definition. πŸ˜€ (Sorry.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting an EXC_BAD_ACCESS but just can't work out why, any help would be
I would like to work out a number's factorial. My factorial rule is in
Is there any way to find out an accurate difference between two NSDate ?
I'm trying to work out what regular expression I would need to change this
I thought this would be fairly simple but it turns out not to work
I've spent the last few hours finding out why will_paginate wouldn't work on my
Is there a strategy that would work within the current Firebase offering to detect
**EDIT: There are several options below that would work. Please vote/comment according to your
Take the following lines of code that would work fine in a c# asp.net
In my view, I have two date fields like so: <%= f.text_field :start_date %>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.