JavaScript: Add Days to a Date

While working with dates in JavaScript, we often need to change the dates, like adding or subtracting days to date before processing it. While adding days to a date, we expect the month and year values to be incremented accordingly depending on the number of days being added. This article will discuss practical ways to add days to a javascript date.

Add Days to a Date Value using setDate()

According to the local time, Javascript’s setDate() will set the day of the month of the specified Date instance.

Javascript’s getDate() will get the particular day of the month as per the specified date according to local time.

Example:-

Add three days to today’s date

Code:-

//function to add days to a date
 function addDaysToDate(_date,_noOfDays) 
{
 return new Date(_date.setDate(_date.getDate() + _noOfDays));      
}
//usage of the function addDaysToDate
var todayDate = new Date();
console.log("Today's Date: " + todayDate )
console.log("New Date: " + addDaysToDate(todayDate,3));

Output:-

Today's Date: Wed Mar 02 2022 16:40:12 GMT+0530 (India Standard Time)
New Date: Sat Mar 05 2022 16:40:12 GMT+0530 (India Standard Time)

Explanation:-

  • We first get today’s date from the system using the new Date().
  • Access the current day of the month using the getDate() method.
  • The new value of the day of the month is set in the setDate() method. The arguments passed in this method are the date value received in the previous step and the number of days we want to increase (3 in our case). If the value of month and year needs to be updated, they are incremented accordingly by the setDate() method.
  • Finally, the Date() constructor creates a new date with the day, month, and year values decided by the number of days being added.

Add Days to a Date Value using setTime()

Javascript’s setTime() will set the Date object to the time represented by the milliseconds since January 1, 1970, 00:00:00

Javascript’s getTime() will get the number of milliseconds since ECMASCRIPT epoch

Example:-

Add three days to today’s date

Code:-

//function to add days to a date
function addDaysToDate(_date,_noOfDays) 
{
 addMilliseconds = _noOfDays * 24 * 60 * 60 * 1000;
 return new Date(_date.setTime(_date.getTime() +  (addMilliseconds)));
}
//usage of the function addDaysToDate
var todayDate = new Date();
console.log("Today's Date: " + todayDate )
console.log("New Date: " + addDaysToDate(todayDate,3));

Output:-

Today's Date: Wed Mar 02 2022 17:12:45 GMT+0530 (India Standard Time)
New Date: Sat Mar 05 2022 17:12:45 GMT+0530 (India Standard Time)

Explanation:-

  • We first get today’s date from the system using the new Date().
  • Access the number of milliseconds representing a particular date with the method getTime().
  • In the variable addMilliseconds, we add the number of milliseconds received in the above step and the milliseconds’ value for the number of days (_noOfDays * 24 * 60 * 60 * 1000) we wish to add in a particular date.
  • The addMilliseconds variable value is then passed as an argument to method setTime(). If the value of month and year needs to be updated, they are incremented accordingly by the setTime() method.
  • Finally, the Date() constructor creates a new date.

Add Days to a Date Value by Accessing Properties of a Date

Javascript’s getFullYear() method will return the year of the date specified according to the local time.

Javascript’s getMonth() method will return the month of the date specified according to the local time. The month value is zero-based that is zero indicates the first month of the year.

Javascript’s getDate() method will return the day of the month of the date specified according to the local time.

Javascript’s toDateString() method will return the portion of Date in English in the format:

  • First four letters for the day of the weekday.
  • First three letters of the month’s name.
  • Two digits of the day of the month.
  • Four digits for the year.

Example:-

Add three days to today’s date

Code:-

//function to add days to a date
function addDaysToDate(_date,_noOfDays) 
 {
        var yearFromDate = _date.getFullYear();
        var monthFromYear = _date.getMonth();
        var dayFromYear = _date.getDate();
        var newDate = new Date(yearFromDate, monthFromYear , _noOfDays +  dayFromYear);
        return newDate.toDateString();
 } 
//usage of the function addDaysToDate
var todayDate = new Date();
console.log("Today's Date: " + todayDate.toDateString() )
console.log("New Date: " + addDaysToDate(todayDate,3));

Output:-

Today's Date: Wed Mar 02 2022
New Date: Sat Mar 05 2022

Explanation:-

  • We first get today’s date from the system using the new Date().
  • Then we access different properties of the date.
  • getFullYear() method gets the year from today’s date.
  • getMonth() method receives the current month from today’s date.
  • getDate() method receives the day from today’s date.
  • After accessing all the above values, create a new date by Date() constructor, which sets the value of the day, month, and year based on how many days we are adding to the current date.

I hope this article helped you add the number of days to a javascript date. Good Luck !!!

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top