How to add days to date in JavaScript? [SOLVED]


JavaScript

Introduction

Adding days to a date in JavaScript is a simple operation when using the built-in Date object. All you need to do is create a new Date object and pass in the current date plus the number of days you want to add. For example, how would you add 10 days to the current date? In this article, we will discuss the approach you can take to achieve this.

 

Different methods to add days to date in JavaScript

Method-1: Use setDate() method

To add days to date, we need to obtain the current Date object, then use the setDate() method to add the number of days we want to the Date object.

For example, if we want to add 5 days to the current date, you can use the Date() constructor to get the current date and time value, then use the getDate() method - a getter function - to obtain just the numeric day value and add 5 to it, and use the setDate() method - a setter function - to set the new day value.

const date = new Date();
const days = 5;
console.log(date);

date.setDate(date.getDate() + days);
console.log(date);

Output

2022-11-09T20:21:38.084Z
2022-11-14T20:21:38.084Z

We can create an instance method - addDay - that add days to date in JavaScript using the setDate() method which makes it easier to make use of in the long run.

Date.prototype.addDays = function (days) {
    let date = new Date(this.valueOf());
    date.setDate(date.getDate() + days);
    return date;
};

const date = new Date();
const days = 5;
console.log(date);
console.log(date.addDays(days));

Output

2022-11-09T20:45:52.669Z
2022-11-14T20:45:52.669Z

 

Method-3: Use setTime method

Instead of using the setDate and getDate method, we can directly make use of the getTime method and add the number of minutes we want (which is equivalent to the number of days) and create a new Date object based on the calculation.

For example, if we want to add 5 days to the current date, we can obtain the current date, extract the time value (getTime method), add the number of minutes that corresponds to 5 days (5 * the number of minutes in a day - 86400000) and create a new Date object based on the calculation.

const days = 5;
let newDate = new Date().getTime() + days * 86400000;
let properNewDate = new Date(newDate);

console.log(newDate);
console.log(properNewDate)

Output

1668523006708
2022-11-15T14:36:46.708Z

The newDate held the timestamp of the current days plus five days, and the properNewDate held the correct date and time formatting of the newDate value.

 

Summary

With the getDate, setDate methods and simple addition, we can add days to date (Date objects) in JavaScript without any issues and with ease. In addition, you can make use of the getTime method with a simple addition to add days to date value. Also, we can create an instance method based on the said methods.

 

References

Date.prototype.getDate() - JavaScript | MDN (mozilla.org)
Date.prototype.setDate() - JavaScript | MDN (mozilla.org)

 

Olorunfemi Akinlua

Olorunfemi Akinlua

He is boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs user-friendly interfaces to enhance digital experiences across various domains. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment