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

1. Using setDate() Method

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.

Javascript
// 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

2. Using getTime() Method

The getTime() method in JavaScript returns the number of milliseconds elapsed since January 1, 1970. To add a specific number of days to a date, we first convert the days into milliseconds and then add them to the current date represented in milliseconds. This approach enables precise date manipulation in JavaScript.

Example: This example shows the use of the above-explained approach.

Javascript
// Function to Add days to current date
function addDays(date, days) {
    const newDate = new Date(date.getTime() 
        + days * 24 * 60 * 60 * 1000);
  
    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

Contact Us