We will learn how to increment a date by 1 month using JavaScript using two different methods. Each method is rather straightforward, and you can choose whichever you want.
Increment a date by 1 month using JavaScript
Using getMonth() and setMonth()
The getMonth() method can act on a date object in JavaScript and then return the number of the month minus 1 (e.g. 0 is January and 8 is September). We can increment a date by 1 month by using the setMonth() method with the parameter value equal to the current month (get this by the getMonth() method) plus 1.
Syntax:
setMonth(monthValue)
Parameters:
- monthValue: the integer value of the corresponding month starting from 0 (January)
The setMonth() method sets the month according to the passed month value for the date object. This is also considered the most effective method:
let today = new Date();
let month = today.getMonth();
let next_month = month+1;
console.log(today) //before increment
today.setMonth(next_month)
console.log(today) // result after increment
Output:
Fri Sep 30 2022 16:30:41 GMT+0700
Sun Oct 30 2022 16:30:41 GMT+0700
The above example uses the current date (30/09/2022) and after adding 1 month to the date, the result we receive is 30/10/2022 as expected. However, if
- the current day is larger than 29 and the next month is February, or
- the current day is 31, but next month only has 30 days
we may get unexpected results of incrementing by 2 months. For example:
let today = new Date('2022-01-30');
console.log(today) //before increment
today.setMonth(today.getMonth()+1)
console.log(today) // result after increment
Output:
Sun Jan 30 2022 07:00:00 GMT+0700
Wed Mar 02 2022 07:00:00 GMT+0700
Another example:
let today = new Date('2022-08-31');
console.log(today) //before increment
today.setMonth(today.getMonth()+1)
console.log(today) // result after increment
Output:
Wed Aug 31 2022 07:00:00 GMT+0700
Sat Oct 01 2022 07:00:00 GMT+0700
Dealing with unexpected 2 month increment
This problem often occurs because the current day is nearly the end of the month. For example, if today is 30 January, you will not be able to increment 1 month just by using the above approach because the next month is February, it only has 28 or 29 days. However, we can make our own function to cope with these specific situations:
Syntax:
setMonth(monthValue,dayValue)
Parameters:
- monthValue: the integer value of the corresponding month starting from 0 (January)
- dayValue: A number between 1 and 31, represents the day of the month.
The setMonth() method sets the month for a specified date according to the currently set year. Our function is like:
function incrementMonth(today) {
let year = today.getYear()
if (today.getMonth() == 0 && today.getDate() >= 29)
if (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0))
// If leap year then the end of February is 29, otherwise 28
today.setMonth(today.getMonth() + 1, 29)
else today.setMonth(today.getMonth() + 1, 28)
else if ((today.getMonth() == 2 || today.getMonth() == 4 || today.getMonth() == 7 || today.getMonth() == 9) &&
today.getDate() == 31)
today.setMonth(today.getMonth() + 1, 30)
}
Sample test case:
let today = new Date('2020-08-31');
console.log(today) //before increment
incrementMonth(today)
console.log(today) // result after increment
Output:
Mon Aug 31 2020 07:00:00 GMT+0700
Wed Sep 30 2020 07:00:00 GMT+0700
Another test case:
let today = new Date('2020-01-31');
console.log(today) //before increment
incrementMonth(today)
console.log(today) // result after increment
Output:
Fri Jan 31 2020 07:00:00 GMT+0700
Sat Feb 29 2020 07:00:00 GMT+0700
We created a new function named incrementMonth to handle specific months to ensure the date is exactly incremented by 1 month instead of 2 month. To use the function all you have to do is declare it as our example above and then call the function with the parameter is the Date object.
Summary
We have learned how to increment a date by 1 month using JavaScript. If you want to increment 30 days (a normal month length) to every date then the first approach is better for you. Otherwise, if you want to increment exactly 1 month then you can consider our second approach.
Maybe you are interested:
- Get the GMT Hours using JavaScript
- Get the current Date and Time in Seconds using JavaScript
- Get the current year for copyright using javascript
I’m Edward Anderson. My current job is as a programmer. I’m majoring in information technology and 5 years of programming expertise. Python, C, C++, Javascript, Java, HTML, CSS, and R are my strong suits. Let me know if you have any questions about these programming languages.
Name of the university: HCMUT
Major: CS
Programming Languages: Python, C, C++, Javascript, Java, HTML, CSS, R