How To Add Days To A Date In JavaScript?

In this article, we are going to learn how to add days to a date in JavaScript.

Before that, let’s see what is a date object.

What Is Date?

It is an in-built object in JavaScript which is used to reference a certain point of time. By default, new Date() creates a Date object with the current date and time on the machine. We make the new object refer to our own timestamp by passing in the time as argument in the constructor function.

You can view the latest Mozilla Documentation Here.

How To Create An Instance Of Date?

These are some examples which show how you can create a date –

const today = new Date()
const newYears = new Date('January 1, 2023');

console.log(today.toString()) 
// 'Wed Sep 14 2022 17:37:10 GMT+**** (*** Standard Time)'

console.log(newYears.toString())
// Sun Jan 01 2023 00:00:00 GMT+**** (*** Standard Time)

Explanation: We Created 2 new date objects, first of the current time, second of the New Year of 2023.

💡 You can pass a date string in the constructor to create a new Date object.

How To Add Days To A Date In JavaScript?

how to add days to a date in JavaScript

We can use getDate() and setDate() to achieve our required functionality.

Steps Involve:

  • Get the current date
  • Add X days to it
  • Save the new date

getDate() returns the current date of the month for the defined date object.

This returns a number from 1 to 31. As these are the only possible values for days in a month.

Syntax :

myDate.getDate()

setDate() is used to set a date on the date object.

This requires a number from 1 – 31 but you can pass other values too. This allows us to achieve more functionality.

  • 0 will set the date to the last date of previous month
  • -1 will set the date the day before the last day of the previous month
  • Other negative values follow the same trend
  • Any values greater than the current month’s max days will move the date object to the next month

Syntax :

myDate.setDate(days)
// days is a required parameter

Let’s see how you can achieve the desired functionality –

const myDate = new Date();
const x = 1;

myDate.setDate(myDate.getDate() + x);
// adding and saving

Explanation : In this example, we get the current date, add x days to it and use the date.setDate method to save the values.

💡 Note : Javascript automatically handles (negative / greater than current month’s max) dates.
const myDate = new Date('2022-08-10')
// this references 10th Aug 2022

myDate.setDate(myDate.getDate() + 30);
// we are adding 30 to 10

console.log(myDate);
// Fri Sep 09 2022 05:30:00 GMT+0530
console.log(myDate.toLocaleString());
// prints the date in your local language
// '9/9/2022, 5:30:00 AM' in my case

Explanation : JavaScript automatically moves the date object one month forward and sets the appropriate date when the date overflows the current month’s max.

The solution in functional format will look like this:

function addDays(date,x){
	date.setDate(date.getDate() + x);
	return date
}

addDays(new Date(),10);
// this will return a date object 10 days ahead of current date

Summary

In this article, we learned about Date objects and how it’s very easy to handle dates in JavaScript, thanks to the many built-in methods and functions. We also saw how you can add days to a date object by using setDate and getDate. You need to get the current date first, add x days and then save the value.

Leave a Reply