How to add days to current date in JavaScript

Sep 28, 2023#javascript#how-to

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.

Using getDate and setDate methods

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.

Using getTime and setTime methods

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

Using date time libraries

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.

  • Day.js (44.2k ⭐): Similar to Moment.js but much smaller in size. It also supports plugins for additional features such as custom parsing, relative time, duration, calendar, and locale.
  • date-fns (32.3k ⭐): It provides over 200 functions for manipulating dates and times, such as format, parse, add, subtract, compare, and difference. It also supports time zones and localization with over 60 languages.
  • Luxon (14.3k ⭐): It supports time zones, locales, and formatting options out of the box without requiring any additional libraries or plugins.

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