If you want to add 7 days to date, then follow the following steps:
getDate()
method on the Date
object to get the day of the month.setDate()
method.In this way, you can add 7 days to a date.
let today = new Date(); console.log(today); //Sun Feb 20 2022 today.setDate(today.getDate() + 7); console.log(today); //Sun Feb 27 2022
Date
class provides two methods, getDate()
and setDate()
.
The getDate() method is used to get the day of the month. It returns an integer value between 1 and 31.
The setDate() method is used to set the day of the month.
If you are worried about adding 7 days to the date result into the following month or year, you have to update month and year. Then no need to worry; the good thing about the Date class is that it automatically updates the month or year based on the day passed by the user.
let d1 = new Date('2022-01-27'); d1.setDate(d1.getDate() + 7); console.log(d1.toString()); //Thu Feb 03 2022 let d2 = new Date('2021-12-25'); d2.setDate(d2.getDate() + 7); console.log(d2.toString()); //Sat Jan 01 2022