How to add months to a date in JavaScript?
Corrected as of 25.06.2019:
var newDate = new Date(date.setMonth(date.getMonth()+8));
Old
From here:
var jan312009 = new Date(2009, 0, 31);
var eightMonthsFromJan312009 = jan312009.setMonth(jan312009.getMonth()+8);
JavaScript function to add X months to a date
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 2017console.log(addMonths(new Date(2016,1,29),12).toString());
// Subtract 1 month from 1 Jan 2017 -> 1 Dec 2016console.log(addMonths(new Date(2017,0,1),-1).toString());
// Subtract 2 months from 31 Jan 2017 -> 30 Nov 2016console.log(addMonths(new Date(2017,0,31),-2).toString());
// Add 2 months to 31 Dec 2016 -> 28 Feb 2017console.log(addMonths(new Date(2016,11,31),2).toString());
Adding months to date in js
By calling start.setMonth
you end up updating the month on both dates. (This is noted in one of the comments on the answer you followed, but not in the answer itself.)
Separate the statements to only affect the date you want changed:
let start = new Date();
let end = new Date(start);
end.setMonth(end.getMonth() + 1);
console.log(start, end)
Add months to a date is not working in JavaScript
You should place new Date(addDate).getMonth() + 1
in parentheses. You are building a string and since you don't provide explicit precedence, first new Date(addDate).getMonth() gets added, then 1 gets concatenated to the string.
Try this:
var startDateFormat = new Date(2018, 11, 24);
var addDate = startDateFormat.setMonth(startDateFormat.getMonth() + 45);
console.log(new Date(addDate).getFullYear() + "-" + (new Date(addDate).getMonth() + 1) + "-" + new Date(addDate).getUTCDate())
Add months in local language to date function
var $time = "Fri May 04 20:33:17 +0000 2018";
function relative_time(time_value) {
var date = new Date(time_value);
var months= ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio',
'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'];
return date.getDate()+ ' '+ months[date.getMonth()];
}
relative_time($time);
Does this answer your question?
Add N months to YYYY-MM date in JavaScript
The main challenge is due to the fact that we count months between 1 and 12, while a modulo of 12 have a valid range of 0 to 11.
We can solve this by first subtracting 1 from the month count so that its range can fit within the modulo's range, and add the 1 back after the modulo calculation.
function addMonths(time, monthsToAdd) {
const offsetCycles = Math.ceil(Math.abs(monthsToAdd) / 12);
const zeroBasedMonth = time.month + monthsToAdd - 1 + offsetCycles * 12;
const additionalYears = Math.floor(zeroBasedMonth / 12) - offsetCycles;
return {
year: time.year + additionalYears,
month: (zeroBasedMonth % 12) + 1,
};
}
console.log([
addMonths({ year: 2001, month: 1 }, 2),
addMonths({ year: 2001, month: 11 }, 2),
addMonths({ year: 2001, month: 11 }, 1),
addMonths({ year: 2001, month: 11 }, 12),
addMonths({ year: 2001, month: 11 }, 14),
addMonths({ year: 2001, month: 1 }, -1),
addMonths({ year: 2001, month: 1 }, -13),
]);
Related Topics
Unexpected Token Colon JSON After Jquery.Ajax#Get
In JavaScript, Why Is the "This" Operator Inconsistent
Axios Posting Params Not Read by $_Post
HTML Input Type=File, Get the Image Before Submitting the Form
Open Ie Browser in Firefox/Chrome Page
Text Wrap in a <Canvas> Element
Disable Dragging an Image from an HTML Page
Determine Original Name of Variable After Its Passed to a Function
Why Is My Infinite Loop Blocking When It Is in an Async Function
Remove Property for All Objects in Array
What Does Curly Brackets in the 'Var { ... } = ...' Statements Do
Jquery Ajax Call to PHP Script with JSON Return
How to Manually Trigger Validation with Jquery Validate