In this article, we are going to learn how you can add weeks to a date object in JavaScript. We will take a look at the various Date methods like getDate and setDate which will allow us to achieve the required functionality.
Before that, let’s see what is a date object.
What Is Date?
It is an in-built object in JavaScript which is use to reference a certain point of time. By default, new Date() creates a Date object with the current date and time on the machine. We make the new object refer to our own timestamp by passing in the time as argument in the constructor function.
You can view the latest Mozilla Documentation Here.
How To Create An Instance Of Date?
These are some examples that show how you can create a date –
const today = new Date()
const newYears = new Date('January 1, 2023');
console.log(today.toString())
// 'Wed Sep 14 2022 17:37:10 GMT+**** (*** Standard Time)'
console.log(newYears.toString())
// Sun Jan 01 2023 00:00:00 GMT+**** (*** Standard Time)
Explanation: We Created 2 new date objects, first of the current time, second of the New Year of 2023.
💡 You can pass a date string in the constructor to create a new Date object.
Date Methods That We Will Use
getDate() returns the current date of the month for the defined date object.
This returns a number from 1 to 31. As these are the only possible values for days in a month.
Syntax :
myDate.getDate()
setDate() is used to set a date on the date object.
This requires a number from 1 – 31 but you can pass other values too. This allows us to achieve more functionality –
- 0 will set the date to the last date of previous month
- -1 will set the date the day before the last day of the previous month
- Other negative values follow the same trend
- Any values greater than the current month’s max days will move the date object to the next month
Syntax :
myDate.setDate(days)
// days is a required parameter
How To Add Weeks To A Date Object In JavaScript
We can use getDate() and setDate() to add weeks to a date object in JavaScript.
Steps Involved:
- Get the current date
- Add required number of days
- Save the new date
Let’s see how you can achieve the desired functionality, here we are adding a single week.
const myDate = new Date();
const x = 7;
myDate.setDate(myDate.getDate() + x);
// adding and saving
console.log(myDate);
// this will print the date 7 days ahead of current date
Explanation : In this example, we get the current date, add 7 days to it and use the date.setDate method to save the values.
The above code in functional form will be:
function addWeek(date){
date.setDate(date.getDate() + 7);
return date
}
For adding multiple weeks, you can add another parameter to accept the number of weeks to add –
function addWeeks(date,no_of_weeks){
date.setDate(date.getDate() + 7*no_of_weeks)
return date
}
addWeeks(new Date(),5);
// this will add 5 weeks to the current date object
Overflow Date When Setting Value
💡 Note : JavaScript automatically handles (negative / greater than current month’s max) dates.
const myDate = new Date('2022-08-10')
myDate.setDate(myDate.getDate() + 30);
// we are adding 30 to 10
console.log(myDate);
// Fri Sep 09 2022 05:30:00 GMT+0530
console.log(myDate.toLocaleString());
// prints the date in your local language
// '9/9/2022, 5:30:00 AM' in my case
Explanation : JavaScript automatically moves the date object one month forward and sets the appropriate date.
Summary
In this article, we learned about Date objects and how it’s very easy to handle dates in JavaScript, thanks to the many built-in methods and functions. We also saw how you can add weeks to a date object in JavaScript by using setDate and getDate. You need to get the current date first, add x days and then save the value.