JavaScript Add Days to Date


In JavaScript, dealing with dates often involves manipulating them by adding or subtracting specific time units.

Adding days to a Date object is a common requirement in many applications, from scheduling tasks πŸ—“οΈ to calculating future dates for events.

    Table of Contents

  1. Using setDate() Method
  2. Using getTime() and setTime() Methods
  3. Using the Date Constructor
  4. Using Libraries like date-fns or moment.js
  5. Conclusion

1. Using setDate() Method

The setDate() method of the Date object allows us to directly modify the day πŸ“… of the month.

The idea is to get the current day of the month using the getDate() method and then add the number of days you want to add to the setDate() method.

Let's add 5 days to the current date.

Example

const currentDate = new Date();
console.log(currentDate);

// πŸ“… Adding 5 days
currentDate.setDate(currentDate.getDate() + 5);

console.log(date);

Current date has been modified to 5 days ahead of the current date.


2. Using getTime() and setTime() Methods

Another method to add days to a date is by using the getTime() and setTime() methods.

This approach involves converting the date to milliseconds using getTime(), adding the required days in milliseconds, and then setting the updated time using setTime().

Example

const currentDate = new Date();
console.log(currentDate);

// πŸ“… Adding 5 days
currentDate.setTime(currentDate.getTime() + (5 * 24 * 60 * 60 * 1000));

console.log(currentDate);

3. Using the Date Constructor

The Date constructor, we create a new date object with the specified days added to the current date πŸ“†.

The following example adds 5 days to the current date using the Date constructor.

Example

const currentDate = new Date();
console.log(currentDate);

// πŸ“… Adding 5 days
const newDate = new Date(currentDate.getTime() + (5 * 24 * 60 * 60 * 1000));

console.log(newDate);

4. Using Libraries like date-fns or moment.js

πŸ“š Libraries such as date-fns or moment.js offer dedicated functions for easy date manipulation.

Using date-fns

The addDays() function of the date-fns library allows us to add days to a date object.

var { addDays } = require('date-fns');

const currentDate = new Date();
console.log(currentDate);

// πŸ“… Adding 5 days
const newDate = addDays(currentDate, 5);

console.log(newDate);

Using moment.js

To add days to a date using moment.js, use the add() function as shown below.

var moment = require('moment');

const currentDate = new Date();
console.log(currentDate);

// πŸ“… Adding 5 days
const newDate = moment(currentDate).add(5, 'days').toDate();
console.log(newDate);

Conclusion

Adding days to a JavaScript Date object involves various techniques, each with its own advantages.

Whether using native methods like setDate() or getTime() with setTime(), or leveraging third-party libraries like date-fns or moment.js, mastering these methods enables efficient management of day-based operations within your JavaScript applications.