published: 23 Apr 2022
2 min read
How to add months to a date in JavaScript
To add months to date in JavaScript:
- 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 bygetMonth()
plus the number of months you want to add. - The
setMonth()
method sets the value on the given date.
Here is an example that adds 2 months to the current date in JavaScript:
const date = new Date()
date.setMonth(date.getMonth() + 2)
console.log(date)
// Thu Nov 10 2022 23:02:54 GMT+0500 (Pakistan Standard Time)
The getMonth()
method returns a number between 0
(January) and 11
(December), presenting the number of months of the given date.
The setMonth()
method takes a zero-based integer representing the month of the year and sets the value on the date.
The above methods also take care of the situation where adding a specific number of months to date results in the next day, month, or year:
const date = new Date('2022-09-10T23:33:45.900Z')
date.setMonth(date.getMonth() + 6)
console.log(date.toUTCString())
// Fri, 10 Mar 2023 23:33:45 GMT
If you frequently need to add months to date, create a reusable function that takes the number of months as a parameter and adds them to the current date, as shown below:
Date.prototype.addMonths = function (months) {
const date = new Date(this.valueOf())
date.setMonth(date.getMonth() + months)
return date
}
const date = new Date('2022-09-10T16:23:45.900Z')
// Add 3 months
const result = date.addMonths(3)
console.log(result.toUTCString())
// Sat, 10 Dec 2022 16:23:45 GMT
In the above example, we added a function called addMonths()
to the Date
object prototype. This function will be available to all instances of Date
for adding months to date.
Are we missing something? Help us improve this article. Reach out to us.
How to add months to a date in JavaScript
To add months to date in JavaScript:
- 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 bygetMonth()
plus the number of months you want to add. - The
setMonth()
method sets the value on the given date.
Here is an example that adds 2 months to the current date in JavaScript:
const date = new Date()
date.setMonth(date.getMonth() + 2)
console.log(date)
// Thu Nov 10 2022 23:02:54 GMT+0500 (Pakistan Standard Time)
The getMonth()
method returns a number between 0
(January) and 11
(December), presenting the number of months of the given date.
The setMonth()
method takes a zero-based integer representing the month of the year and sets the value on the date.
The above methods also take care of the situation where adding a specific number of months to date results in the next day, month, or year:
const date = new Date('2022-09-10T23:33:45.900Z')
date.setMonth(date.getMonth() + 6)
console.log(date.toUTCString())
// Fri, 10 Mar 2023 23:33:45 GMT
If you frequently need to add months to date, create a reusable function that takes the number of months as a parameter and adds them to the current date, as shown below:
Date.prototype.addMonths = function (months) {
const date = new Date(this.valueOf())
date.setMonth(date.getMonth() + months)
return date
}
const date = new Date('2022-09-10T16:23:45.900Z')
// Add 3 months
const result = date.addMonths(3)
console.log(result.toUTCString())
// Sat, 10 Dec 2022 16:23:45 GMT
In the above example, we added a function called addMonths()
to the Date
object prototype. This function will be available to all instances of Date
for adding months to date.
Are you looking for other code tips?
JS Nooby
Javascript connoisseur