How to use setDate() Method In Javascript
The setDate() method allows you to set the day of the month for a Date object. First, you create a Date object representing the current date. Then, you can add days to this current date and assign the result to a new date object. This method is useful for manipulating dates in JavaScript.
Example: This example shows the use of the above-explained approach.
// Function to Add days to current date
function addDays(date, days) {
const newDate = new Date(date);
newDate.setDate(date.getDate() + days);
return newDate;
}
// Get the current date
const todayDate = new Date();
// Number of days that we want to
// add in current date
const days = 7;
// Function call to add days
const newDate = addDays(todayDate, days);
console.log("New Date: ", newDate.toDateString());
Output
New Date: Sun Dec 17 2023
How to Add Days to Date in JavaScript?
You will learn how to add days to a date in JavaScript. We’ll focus on adding days to the current date. There are two methods to achieve this with the Date object:
Table of Content
- Using setDate() Method
- Using getTime() Method
Contact Us