No date mutatation:

With this code, the date variable won't be changed when creating newDate.

var date = new Date();
console.log(date);

var newDate = new Date(new Date(date).setMonth(date.getMonth() + 8));
console.log(newDate);

Corrected as of 25.06.2019:

var date = new Date();
console.log(date);

var newDate = new Date(date.setMonth(date.getMonth() + 8));
console.log(newDate);

Old From here:

var jan312009 = new Date(2009, 0, 31);
var eightMonthsFromJan312009  = new Date(jan312009.setMonth(jan312009.getMonth()+8));
Answer from Daniel Protopopov on Stack Overflow
Top answer
1 of 3
522

No date mutatation:

With this code, the date variable won't be changed when creating newDate.

var date = new Date();
console.log(date);

var newDate = new Date(new Date(date).setMonth(date.getMonth() + 8));
console.log(newDate);

Corrected as of 25.06.2019:

var date = new Date();
console.log(date);

var newDate = new Date(date.setMonth(date.getMonth() + 8));
console.log(newDate);

Old From here:

var jan312009 = new Date(2009, 0, 31);
var eightMonthsFromJan312009  = new Date(jan312009.setMonth(jan312009.getMonth()+8));
2 of 3
134

I took a look at the datejs and stripped out the code necessary to add months to a date handling edge cases (leap year, shorter months, etc):

Date.isLeapYear = function (year) { 
    return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0)); 
};

Date.getDaysInMonth = function (year, month) {
    return [31, (Date.isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
};

Date.prototype.isLeapYear = function () { 
    return Date.isLeapYear(this.getFullYear()); 
};

Date.prototype.getDaysInMonth = function () { 
    return Date.getDaysInMonth(this.getFullYear(), this.getMonth());
};

Date.prototype.addMonths = function (value) {
    var n = this.getDate();
    this.setDate(1);
    this.setMonth(this.getMonth() + value);
    this.setDate(Math.min(n, this.getDaysInMonth()));
    return this;
};

This will add "addMonths()" function to any javascript date object that should handle edge cases. Thanks to Coolite Inc!

Use:

var myDate = new Date("01/31/2012");
var result1 = myDate.addMonths(1);

var myDate2 = new Date("01/31/2011");
var result2 = myDate2.addMonths(1);

->> newDate.addMonths -> mydate.addMonths

result1 = "Feb 29 2012"

result2 = "Feb 28 2011"

🌐
Mark Needham
markhneedham.com › blog › 2009 › 01 › 07 › javascript-add-a-month-to-a-date
Javascript: Add a month to a date | Mark Needham
January 7, 2009 - var jan312009 = new Date(2009, 1-1, 31); var oneMonthFromJan312009 = new Date(jan312009).add(1).month(); Sat Jan 31 2009 00:00:00 GMT+1100 (EST) Sat Feb 28 2009 00:00:00 GMT+1100 (EST) There are loads of other useful date manipulation functions which you can read more about on the API, just don’t forget that date in Javascript is mutable so any manipulation done to dates contained in vars will change the original value.
🌐
W3Schools
w3schools.com › jsref › jsref_setmonth.asp
JavaScript Date setMonth() Method
In JavaScript January is 0, February is 1, .... , December is 11. setFullYear() setDate() setUTCDate() setUTCMonth() setUTCFullYear() Date.setMonth(month, day) Set the month to 4 (May) and the day to 20: const d = new Date("2025-01-15"); d.setMonth(4, 20); Try it Yourself » ·
🌐
Futurestud.io
futurestud.io › tutorials › add-month-s-to-a-date-in-javascript-or-node-js
Add Month(s) to a Date in JavaScript or Node.js
/** * Returns the date with added `months` of delay. * * @param {Number} months - the delay in months * * @returns {Date} */ function dateWithMonthsDelay (months) { const date = new Date() date.setMonth(date.getMonth() + months) return date } Notice that date.setMonth directly changes the date instance. A nice benefit of using this built-in method is that JavaScript takes care of switching the unit.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date › setMonth
Date.prototype.setMonth() - JavaScript | MDN
If a parameter you specify is outside of the expected range, other parameters and the date information in the Date object are updated accordingly. For example, if you specify 15 for monthValue, the year is incremented by 1, and 3 is used for month. The current day of month will have an impact on the behavior of this method. Conceptually it will add the number of days given by the current day of the month to the 1st day of the new month specified as the parameter, to return the new date.
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-date-add-months
How to Add Months to a Date in JavaScript | bobbyhadz
Note that the setMonth method mutates the Date object it was called on. If you don't want to change the Date in place, you can create a copy of it before calling the method. ... Copied!function addMonths(date, months) { const dateCopy = new ...
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › add-month-to-date
JavaScript Add Month to Date - Mastering JS
Remember that months are 0-indexed in JavaScript dates; getMonth() returns 0 for January, 1 for February, etc. const rolloverYear = new Date('2022-12-06'); rolloverYear.getMonth(); // 11 · While we recommend using vanilla JavaScript dates for simple operations like adding a month, we understand that many developers use date helper libraries.
🌐
W3Resource
w3resource.com › javascript-exercises › javascript-date-exercise-43.php
JavaScript: Add specified months to a date - w3resource
Write a JavaScript function that adds a specified number of months to a Date object using setMonth() and handles month overflow.
Find elsewhere
Top answer
1 of 3
1
Hello Rachel, here is the updated code to get the date for the next 6 months: var currentDate = new Date();currentDate.setMonth(currentDate.getMonth() + 6);var day = currentDate.getDate();var month = currentDate.getMonth() + 1;var year = currentDate.getFullYear();var player = GetPlayer();var newName = month + "/" + day + "/" + year;player.SetVar("DateValue", newName); You can use the setMonth method of the Date object to add 6 months to the current date and then we extract the day, month, and year of the new date and update the Date Value. Modifying the "+6" in the second line will get your desired future date.
2 of 3
0
Basically you just add the future day in your Date Object. Like this.var someFutureDate = new Date('11/21/2023');To format it as you want you have to repeat similar steps as you do in the above code.However you can use builtin functionality of the Date Object to format it too.I guess in the end you want to compare whether a date is past or not.For that you need to use getTime() of the Date Object. It translates any given Date to a number of milliseconds that have passed since Januari 1 1970. And thus you then can compare 2 dates...Like this.// Compare the two dates using getTime() method and check if the given date is in the past var currentDate  = new Date();var futureDate    = new Date('11/22/2024 12:30 PM');var pastDate       = new Date('11/21/2021 18:30 PM'); if (futureDate.getTime() > currentDate.getTime()) { }if (pastDate.getTime() < currentDate.getTime()) {}getTime() returns a number like this 845337932000   Adding a simple sample Story.
🌐
Attacomsian
attacomsian.com › blog › javascript-date-add-months
How to add months to a date in JavaScript
September 10, 2022 - Use the getMonth() method to get a zero-based value for the months of the given date. Use the setMonth() method by passing the result returned by getMonth() plus the number of months you want to add.
🌐
JSDev
jsdev.space › howto › add-months-to-date-js
How to Add Months to a Date in JavaScript
import { addMonths } from "date-fns"; import { utcToZonedTime, format } from "date-fns-tz"; const utc = addMonths(new Date("2026-01-12T15:00:00.000Z"), 1); const zone = "America/New_York"; const local = utcToZonedTime(utc, zone); console.log(format(local, "yyyy-MM-dd HH:mmXXX")); This helps avoid DST-related formatting issues. Adding months is not just arithmetic.
🌐
npm
npmjs.com › package › @jsbits › add-months
@jsbits/add-months - npm
This example shows the behavior of both methods using the same date instance: // this is using ESM syntax, but you can use require() import '@jsbits/add-months/proto' // helpers const showLoc = (dt) => console.log('' + dt) const showUTC = (dt) => console.log(dt.toJSON()) const showRes = (dt) => console.log(dt) const date = new Date(2019, 0, 30, 20, 0, 0) showLoc(date) // ⇒ Wed Jan 30 2019 20:00:00 GMT-0600 (CST) showRes(date.addMonths(1)) // ⇒ 1551405600000 showLoc(date) // ⇒ Thu Feb 28 2019 20:00:00 GMT-0600 (CST) showUTC(date) // ⇒ 2019-03-01T02:00:00.000Z showRes(date.addUTCMonths(1)) // ⇒ 1554084000000 showUTC(date) // ⇒ 2019-04-01T02:00:00.000Z
      » npm install @jsbits/add-months
    
Published   Dec 29, 2018
Version   1.1.2
Author   aMarCruz
🌐
Microsoft Learn
learn.microsoft.com › en-us › answers › questions › 964227 › add-12-months-from-selected-date-in-javascript-c
Add 12 months from selected date in javascript c# - Microsoft Q&A
Microsoft 365 and Office | Development | Office JavaScript API · An Office service that supports add-ins to interact with objects in Office client applications. ... A set of technologies in .NET for building web applications and web services. Miscellaneous topics that do not fit into specific categories. ... Do that using a loop that iterates 12 times. (Not tested code) //Given the start date, add a row for 12 months from that day function SaveAddFiscalPeriod( startDate ) { //Convert to a date type - assuming first of month otherwise you'll need to adjust the day to 1 var start = Date.parse(startDate); //Note that setMonth modifies `start` so it jumps 1 month each time through the loop for (let index = 0; index < 12; ++index) { gvDT.row.add([ ID, start, new Date(start.setMonth(date.getMonth()+1)) ]); } }
🌐
TutorialsPoint
tutorialspoint.com › How-to-add-months-to-a-date-in-JavaScript
How to add months to a date in JavaScript?
Note ? The formatting of the date is MM/DD/YYYY. In this tutorial, we have discussed how to add months to a date. We use two methods for this purpose. One is the getMonth() method to get the month of the date.
🌐
Envato Tuts+
code.tutsplus.com › home › javascript
How to Add or Subtract Days (And More) From Dates in Vanilla JavaScript | Envato Tuts+
June 8, 2023 - This tutorial will teach you how to add or subtract years, months, days, hours, minutes, or seconds from a date in vanilla JavaScript. No libraries required!
🌐
Codecademy
codecademy.com › docs › javascript › dates › .setmonth()
JavaScript | Dates | .setMonth() | Codecademy
May 15, 2024 - For example, when given 14 as the parameter for a monthValue, the year is incremented by 1, and the month is set to 2 (March). If no value is provided for the dateValue parameter, the date value will not change unless there is an overflow. For example, calling .setMonth(1) to set the month to February on a Date object that was 1/31/2023 will result in overflow, and the date will be set to 3/3/2023.
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › date-add-days
JavaScript Add Days to Date - Mastering JS
JavaScript Dates have a setDate() function that sets the current date of the month. This function handles rolling over months, so setDate(32) will increment the date's month and set the correct date. For example, here's how you can add 30 days ...
🌐
Javascripts
javascripts.com › add-months-to-dates-in-javascript
How to Add Months to Dates in JavaScript
June 23, 2023 - The provided JavaScript code will ... date by using the getUTCMonth() method to retrieve the current month of the date, and then the setUTCMonth() method to set a new month that is the sum of the current month and the number of months to add...
🌐
Delft Stack
delftstack.com › home › howto › javascript › javascript add month to date
How to Add Months to a Date in JavaScript | Delft Stack
February 2, 2024 - The package overrides the JavaScript Date object. It provides a method add() through which we can specify the number to be added. Then, we can specify what is to be added by calling methods like month(), day(), etc.
🌐
Moment.js
momentjs.com › docs
Moment.js | Docs
Manipulate Add Subtract Start of Time End of Time Maximum Minimum Local UTC UTC offset Time zone Offset · Display Format Time from now Time from X Time to now Time to X Calendar Time Difference Unix Timestamp (milliseconds) Unix Timestamp (seconds) Days in Month As Javascript Date As Array ...