How to Add Months to a Date in JavaScript
-
Use the
setMonth()
andgetMonth()
Methods of theDate
Object to Add Months to a Date in JavaScript -
Use
date.js
to Add Months to a Date in JavaScript -
Use the
moment
Library to Add Months to a Date in JavaScript
This tutorial will introduce some methods to add specific months to a date in JavaScript.
Use the setMonth()
and getMonth()
Methods of the Date
Object to Add Months to a Date in JavaScript
We can use the combination of the setMonth()
and getMonth()
methods provided by the Date
object to add a month to a date in JavaScript. The getMonth()
method returns the month from the specified date.
The returned month is in zero-based value, which means the first month is denoted as 0
. Similarly, the setMonth()
method sets the specified months to a date.
We can use the functionality of these two methods together and add a specific number of months to a date. We can use the getMonth()
method to get the current month, add the number of months and use the setMonth()
method to set the new month.
Let’s add one month to the current date using these methods.
For example, get the current date using the Date
object and store it in the date
variable. Next, invoke getMonth()
using the date
object and 1
.
Next, use this expression as a parameter to the setMonth()
method. Invoke the setMonth()
method with the date
object.
Finally, set the whole expression as a parameter to a Date
object. Print both dates.
Example Code:
var date = new Date();
console.log(date)
var newDate = new Date(date.setMonth(date.getMonth() + 1));
console.log(newDate)
Output:
2023-01-31T14:14:09.646Z
2023-03-03T14:14:09.646Z
The current date is 2023-01-31
, and after 1
month is added, it becomes 2023-03-03
. This way, we can use the setMonth()
and getMonth()
methods to add months to date in JavaScript.
Use date.js
to Add Months to a Date in JavaScript
In JavaScript, we can also use the date.js
package to add a specific number of months to a date. 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.
Therefore, we can specify the number of months to be added in the add()
method and then call the month()
method.
First, install the package using the following command.
npm i datejs
For example, get the current date using the Date.today()
method and store it in the currDate
variable. After that, use the currDate
variable to call the add()
method and supply 1
as the parameter.
Next, invoke the month()
method. Finally, print both dates.
Example Code:
require('datejs')
var currDate = Date.today();
console.log(currDate)
var newDate = currDate.add(1).month();
console.log(newDate)
Output:
2023-01-30T18:15:00.000Z
2023-02-27T18:15:00.000Z { _orient: 1 }
Instead of calling add(1).month()
, you can also call the addMonths(1)
method. It will yield the same result.
In JavaScript, we can use the date.js
package to add a specific number of months to a date.
Use the moment
Library to Add Months to a Date in JavaScript
The usage of moment
is quite similar to date.js
in terms of adding a month to a date in JavaScript.
The library provides the add()
method, where we can set the number as the first parameter and the item in which the number is added as the second parameter. The second parameter is a string.
Install the library with the following command.
npm i moment
First, require the moment
library and call the moment()
method. Set a Date
object as the parameter of the method.
Next, assign the expression to a currDate
variable. This returns the current date.
Then, use the currDate
variable to invoke the add()
method. In the add()
method, supply the first parameter as 1
and the second as "month"
. Finally, print both dates.
Example Code:
const moment = require('moment')
var currDate = moment(new Date());
console.log(currDate)
var newDate = currDate.add(1, 'month');
console.log(newDate)
Output:
Moment<2023-01-31T20:37:11+05:45>
Moment<2023-02-28T20:37:11+05:45>
In JavaScript, we can use the moment
library to add a month to the date.
Subodh is a proactive software engineer, specialized in fintech industry and a writer who loves to express his software development learnings and set of skills through blogs and articles.
LinkedIn