To get one month back date in JavaScript,
- Use the
Date()
constructor to get the current date object. - Call the
getMonth()
method on the current date object to get the current month’s index. - Call the
setMonth()
method on the current date object by passing the current month’s index – 1 as an argument to it.
For example,
// Get today's date const today = new Date(); console.log('Before: ', today); // Get current month const month = today.getMonth(); // Set today's date back to previous month today.setMonth(month - 1); // Print new date console.log('After: ', today);
Output:
Before: Sat Mar 18 2023 17:54:29 GMT+0530 (India Standard Time) After: Sat Feb 18 2023 17:54:29 GMT+0530 (India Standard Time)
We have used two built-in date methods in this example, getMonth()
and setMonth()
.
The getMonth()
method returns an integer value between 0 and 11 representing the month of the year for the given date. 0 corresponds to January, 1 corresponds to February, and so on.
When you call the getMonth()
method on the current date object, it gives you the current month’s index(0 based).
For example,
// Today's date const today = new Date() console.log(today.getMonth()); // Output: 👉 2 console.log(new Date('2022-10-15').getMonth()); // Output: 👉 9
The setMonth()
method on the other side sets a month for the specified date.
While using the setMonth()
method you have to keep in mind that months are 0 based.
For example,
// Mar 10, 2023 const date = new Date('2023-03-10'); console.log(date); // Output: 👉 Fri Mar 10 2023 05:30:00 GMT+0530 (India Standard Time) // Set month to January date.setMonth(0); console.log(date); // Output: 👉 Tue Jan 10 2023 05:30:00 GMT+0530 (India Standard Time)
One of the main advantages of using the setMonth() method is that it automatically adjusts the date if you pass a value that is not between 0 to 11.
For example, if you pass a negative value -1, the date would be set to the last month of the previous year:
// current date const date = new Date(); console.log(date); // Output: 👉 Sat Mar 18 2023 18:30:42 GMT+0530 (India Standard Time) // Automatically adjusts the date date.setMonth(-1) console.log(date); // Output: 👉 Sun Dec 18 2022 18:30:42 GMT+0530 (India Standard Time)
Get One Month Back Date for any Specific Date
If you want to go back by one month for any specific date, you have to first get the date object for that specific date using the Date()
constructor and then use the same logic.
For example,
// Oct 20, 2022 const date = new Date('2022-10-20'); console.log('Before: ', date); // Get month from date const month = date.getMonth(); // Set date to previous month date.setMonth(month - 1); // Print new date console.log('After: ', date);
Output:
Before: Thu Oct 20 2022 05:30:00 GMT+0530 (India Standard Time) After: Tue Sep 20 2022 05:30:00 GMT+0530 (India Standard Time)
If you want to go back by N months, you can use setMonth(monthIdx - N)
, where the monthIdx is the index of the month for the specified date.
That’s all for this article. Thanks for reading!