How to add a week to a date in JavaScript?

Dealing with dates in JavaScript is inevitable. There are so many different situations that require the manipulation of date objects. We frequently use methods to read individual aspects of date and time but there also similar versions of these methods that manipulate the dates.

So, how can you add a week to a date in JavaScript? You can add a week to a Date Object by utilizing the 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 numOfWeeks variable
  • Multiply numOfWeeks by 7 to give total amount of days
  • Use setDate to 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.

 

Proudly published with Gatsby