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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:41:34+00:00 2026-05-14T19:41:34+00:00

I’m looking for the easiest, cleanest way to add X months to a JavaScript

  • 0

I’m looking for the easiest, cleanest way to add X months to a JavaScript date.

I’d rather not handle the rolling over of the year or have to write my own function.

Is there something built in that can do this?

  • 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-14T19:41:34+00:00Added an answer on May 14, 2026 at 7:41 pm

    The following function adds months to a date in JavaScript (source). It takes into account year roll-overs and varying month lengths:

    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());

    The above solution covers the edge case of moving from a month with a greater number of days than the destination month. eg.

    • Add twelve months to February 29th 2020 (should be February 28th 2021)
    • Add one month to August 31st 2020 (should be September 30th 2020)

    If the day of the month changes when applying setMonth, then we know we have overflowed into the following month due to a difference in month length. In this case, we use setDate(0) to move back to the last day of the previous month.

    Note: this version of this answer replaces an earlier version (below) that did not gracefully handle different month lengths.

    var x = 12; //or whatever offset
    var CurrentDate = new Date();
    console.log("Current date:", CurrentDate);
    CurrentDate.setMonth(CurrentDate.getMonth() + x);
    console.log("Date after " + x + " months:", CurrentDate);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.