Add Days To Date in JavaScript

Add Days To Date in JavaScript | The addition of days to a date in JavaScript has several applications. For example, to obtain the date five days from today. You will discover how to add days to a date in JavaScript both with and without the use of external NPM packages in this article. Let’s get moving!

A date object exists in JavaScript. It represents an instant in time in a manner that is independent of platforms. This is accomplished by encoding the date as an object that contains a millisecond-accurate number that represents the time since January 1st, 1970 UTC.

The date object in JavaScript provides a wide variety of functions. The date-related functions do, however, have their own oddities, such as getDay() returning 0–6 for Sunday–Saturday, just like other areas of the language.

Add Days To Date in JavaScript Using setDate()

We will discuss how to use JavaScript’s setDate() method to add days to a date in the following section.

JavaScript’s setDate() method can be used to add days to date by adding the appropriate number of days (or current date). Here are two instances of adding seven days to the given date:

JavaScript Add Days to Date Using setDate() & getDate()

const date = new Date('2025-01-01');
console.log("Actual date: " + date.toDateString());

let days = 7;
date.setDate(date.getDate() + days);
console.log("After adding " + days + " days: " + date.toDateString());

Output:-

Actual date: Wed Jan 01 2025
After adding 7 days: Wed Jan 08 2025

Add Days to Current Date in JavaScript Using setDate() & getDate()

const date = new Date();
console.log("Actual date: " + date.toDateString());

let days = 5;
date.setDate(date.getDate() + days);
console.log("After adding " + days + " days: " + date.toDateString());

Output:-

Actual date: Tue Sep 06 2022
After adding 5 days: Sun Sep 11 2022

There are numerous packages available in NPM that allow you to alter dates in JavaScript. The date-fns package will be used to add days to a date in the next section.

JavaScript Add Days to Date With date-fns

Popular JavaScript date manipulation NPM package Date-fns operates on both the server and browser (Node.js). Any project can have date-fns installed with the npm install date-fns command. After then, it may be utilized to perform a variety of date-related tasks, one of which is to add days to a JavaScript date.

You have to first install the date-fns package. In the terminal type the following command.

npm install date-fns @types/date-fns

Here is an example utilizing date-fns in Node.js to add 7 days to the current date as well as a prior date, similar to what we saw in the previous example:

JavaScript Add Days to Date Using date-fns

const date = new Date('2025-01-01');
console.log("Actual date: " + date.toDateString());

const dateFns = require('date-fns');
let days = 5;
let newDate = dateFns.addDays(date, days);
console.log("After adding " + days + " days: " + newDate.toDateString());

Output:-

Actual date: Wed Jan 01 2025
After adding 5 days: Mon Jan 06 2025

Add Days to Current Date in JavaScript Using date-fns

const date = new Date();
console.log("Actual date: " + date.toDateString());

const dateFns = require('date-fns');
let days = 7;
let newDate = dateFns.addDays(date, days);
console.log("After adding " + days + " days: " + newDate.toDateString());

Output:-

Actual date: Tue Sep 06 2022
After adding 7 days: Tue Sep 13 2022

Common date-related Javascript packages:

Other well-known NPM packages are available to modify and format dates in JavaScript. You can perform a variety of other operations on dates in addition to adding days, such as subtracting days, formatting dates as necessary, dealing with timezones, etc. 

We discussed how to add days to a date in JavaScript using this tutorial. We first added the days using the setDate() native function, and then we repeated the process using date-fns. To give a better picture of what is available, some popular date-related NPM packages quickly competed in a popularity contest. We hope this has provided you with some useful insight.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *