Adding days to the current date in JavaScript is a common task that you may encounter in web development. Whether you want to calculate the expiration date of a cookie, the deadline of a project, or the next birthday of a friend, you need to know how to manipulate dates in JavaScript.
In this article, you will learn how to use the Date object and its methods to easily add days to the current date in JavaScript. You will also see some examples using date time libraries like date-fns.
In Date object, the getDate()
method returns the day of the month for a given date, and the setDate()
method sets the day of the month for a given date.
function addDaysToDate(inputDate, daysToAdd) {
// Create a copy of the input date to avoid modifying it directly
let resultDate = new Date(inputDate);
// Get the day of the month from the input date
let currentDay = resultDate.getDate();
// Add the specified number of days to the current day
let newDay = currentDay + daysToAdd;
// Set the new day of the month
resultDate.setDate(newDay);
// Return the new date
return resultDate;
}
let currentDate = new Date();
console.log(currentDate); // 2023-09-28T03:10:24.001Z
let newDate = addDaysToDate(currentDate, 10);
console.log(newDate); // 2023-10-08T03:10:24.001Z
The setDate()
method handles overflow and underflow cases automatically. This means that if the parameter is outside of the range of valid days for the current month, the function will update the date object accordingly.
For example, if the date object holds June 1st and the parameter is 40, the function will change the date object to July 10th. Similarly, if the parameter is 0, the function will change the date object to May 31st.
In Date object, The getTime()
method returns the number of milliseconds since January 1, 1970, and the setTime()
method sets the date and time by adding or subtracting milliseconds from a given date.
function addDaysToDate(inputDate, daysToAdd) {
// Create a copy of the input date to avoid modifying it directly
let resultDate = new Date(inputDate);
// Get the current time in milliseconds since January 1, 1970
let currentTime = resultDate.getTime();
// Calculate the time to add in milliseconds (daysToAdd * 24 * 60 * 60 * 1000)
let timeToAdd = daysToAdd * 24 * 60 * 60 * 1000;
// Calculate the new time by adding the time to add
let newTime = currentTime + timeToAdd;
// Set the new date and time
resultDate.setTime(newTime);
// Return the new date
return resultDate;
}
let currentDate = new Date();
console.log(currentDate); // 2023-09-28T03:10:24.001Z
let newDate = addDaysToDate(currentDate, 10);
console.log(newDate); // 2023-10-08T03:10:24.001Z
The native Date object in JavaScript has some limitations and quirks that can make it difficult to use in some scenarios. That’s why many developers prefer to use external libraries that provide a more convenient and consistent API for date and time operations.
These libraries can help you perform tasks such as parsing, formatting, comparing, adding, subtracting, and manipulating dates and times in JavaScript. They can also handle time zones, locales, and daylight saving time issues.
Here’s an example using date-fns functions:
const { addDays } = require("date-fns");
const today = new Date();
console.log(today); // 2023-09-28T01:56:00.710Z
// Adding 5 days to current date
const fiveDaysLater = addDays(today, 5);
console.log(fiveDaysLater); // 2023-10-03T01:40:58.000Z
// Adding 10 business days to current date
const tenBusinessDaysLater = addBusinessDays(today, 10);
console.log(tenBusinessDaysLater); // 2023-10-11T01:40:58.000Z