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 8667601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:00:21+00:00 2026-06-12T18:00:21+00:00

Possible Duplicate: Javascript function to add X months to a date I’d like to

  • 0

Possible Duplicate:
Javascript function to add X months to a date

I’d like to add 2 months to a user-input end date. How can I add 2 months to the end date and display it?

/**
* Click "Cal" on end date
*/
function getEndDate() {
if(!document.getElementById('startdate').value.length) {
    alert("Start date must be set first.");
}
else {
    prevEndDate = document.getElementById('enddate').value;
    displayCalendar(document.forms[0].enddate,'yyyy/mm/dd',
        document.getElementById('enddatebutton'));
}
}

/**
* Click "Cal" on expiry date
*/
function getExpiryDate() {
if(!document.getElementById('enddate').value.length) {
    alert("End date must be set first.");
}
else {
    prevExpiryDate = document.getElementById('expirydate').value;
    displayCalendar(document.forms[0].expirydate,'yyyy/mm/dd',
        document.getElementById('expirydatebutton'));
 }
}

Any help is appreciated. Thanks!

  • 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-06-12T18:00:23+00:00Added an answer on June 12, 2026 at 6:00 pm

    You need to get business rules for adding months. The simple solution is:

    function addMonths(dateObj, num) {
      return dateObj.setMonth(dateObj.getMonth() + num);
    }
    

    However, that will change 31 July to 31 September, which will be converted to 1 October. Also, 31 January plus 1 month is 31 February which will be converted to 2 or 3 March depending on whether it’s a leap year or not.

    You might expect the first to be 30 September and the second to be 28 or 29 February (depending on whether it’s a leap year or not).

    So if you want “end of months” be observed, you need to do something like:

    function addMonths(dateObj, num) {
    
        var currentMonth = dateObj.getMonth() + dateObj.getFullYear() * 12;
        dateObj.setMonth(dateObj.getMonth() + num);
        var diff = dateObj.getMonth() + dateObj.getFullYear() * 12 - currentMonth;
    
        // If don't get the right number, set date to 
        // last day of previous month
        if (diff != num) {
            dateObj.setDate(0);
        } 
        return dateObj;
    } 
    

    But check with whoever is responsible for the business rules that that is what they want.

    Edit

    The above works well, but in response to McShaman’s comment here is a version with a simpler check for the month roll–over:

    function addMonths(date, months) {
      var d = date.getDate();
      date.setMonth(date.getMonth() + +months);
      if (date.getDate() != d) {
        date.setDate(0);
      }
      return date;
    }
    
    // Add 12 months to 29 Feb 2016 -> 28 Feb 2017
    console.log(addMonths(new Date(2016,1,29),12).toString());
    
    // Subtract 1 month from 1 Jan 2017 -> 1 Dec 2016
    console.log(addMonths(new Date(2017,0,1),-1).toString());
    
    // Subtract 2 months from 31 Jan 2017 -> 30 Nov 2016
    console.log(addMonths(new Date(2017,0,31),-2).toString());
    
    // Add 2 months to 31 Dec 2016 -> 28 Feb 2017
    console.log(addMonths(new Date(2016,11,31),2).toString());
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to call a JavaScript function from PHP? Can I add a
Possible Duplicate: JavaScript Function Definition in ASP User Control Hi, I have a generic
Possible Duplicate: User control javascript I defined a JavaScript function inside a user control.
Possible Duplicate: JavaScript: Why the anonymous function wrapper? I would like to ask you
Possible Duplicate: How can I get the name of function inside a JavaScript function?
Possible Duplicate: JavaScript: Why the anonymous function wrapper? Sometimes I see a structure like
Possible Duplicate: Run javascript function when user finishes typing instead of on key up?
Possible Duplicate: how can I call javascript function from another project in same solution?
Possible Duplicate: JavaScript Pass Variables Through Reference How can you do something like this
Possible Duplicate: How do you find out the caller function in JavaScript? How can

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.