Here are two ways to add days to date in JavaScript:
- Using setDate() and getDate()
- Using date-fns package’s addDays()
Method 1: Using setDate() and getDate() methods
Use the “getDate()” method on the Date to get the day of the month, then call the “setDate()” method on the Date, passing the sum of getDate() and the number of days to add.
The getDate() method returns the day of the month for a specified date according to local time as a number between 1 and 31.
The setDate() method sets the Date object’s day relative to the current month’s beginning.
Example 1
const date = new Date('2023-04-14'); // Today's date
const daysToAdd = 7; // Number of days to add
date.setDate(date.getDate() + daysToAdd);
console.log(date);
Output
2023-04-21T00:00:00.000Z
This code creates a new Date object with today’s date.
The number of days to add is stored in the daysToAdd variable.
The setDate() method is then called on the date object by adding daysToAdd to the current date’s day of the month using the getDate() method. This updates the date to 7 days in the future. Finally, the updated date is logged into the console.
Example 2
The setDate() method modifies the original Date object, so if you need to keep the original date, you should create a new Date object from it, like this:
const originalDate = new Date('2023-04-14');
const daysToAdd = 7;
const newDate = new Date(originalDate);
newDate.setDate(originalDate.getDate() + daysToAdd);
console.log(originalDate);
console.log(newDate);
Output
2023-04-14T00:00:00.000Z
2023-04-21T00:00:00.000Z
In this code, a new Date object is created from the original date using the new Date(originalDate), and the setDate() method is called on the new date object. The original date object is left unchanged.
Method 2: Using the date-fns package’s addDays() method
Another efficient way to add days to a Date is to use the “addDays()” function from the “date-fns” package.
To use the “date-fns” package, you need to install it using this command: npm install –save date-fns.
Now, you can import it and use its methods.
Example 3
import { addDays } from 'date-fns';
const date = new Date('2023-06-06T00:00:00.000Z');
const newDate = addDays(date, 5);
console.log("New date after adding five days: ", newDate);
console.log("Original Date: ", date);
Output
New date after adding five days: 2023-06-11T00:00:00.000Z
Original Date: 2023-06-06T00:00:00.000Z
That’s it.

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.