setDate()method. It allows you to alter the date object however you see fit.
Let’s add 4 weeks to today’s date:
let numOfWeeks = 4; let totalDays = (numOfWeeks * 7); let todaysDate = new Date(); todaysDate.setDate(now.getDate() + totalDays);
We aren’t doing anything too out of the ordinary here:
- Specify number of weeks in
numOfWeeksby 7 to give total amount of days
setDateto evaluate the result of today’s date
And there you have it when adding any unit of time, always try to convert the unit into days, this helps to add the value to other dates.