- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Increment a given date in JavaScript
In this article, we are going to discuss how to increment a given date using JavaScript. First, we will analyse and understand what is meant by this. Given a certain date x = “05-02-2023”, we want to add y = 7 days to this date and print the resulting date which is “12-02-2023”. As a human we can manually add 7 days to 5th February and get the resultant. But we need JavaScript to do this programmatically for us.
There are few techniques to do so which we shall be discussing in this article.
Using addDays() function
The addDays function takes in 2 parameters which are date and days to be incremented. In the code below we will be adding 7 days to the current date and print the incremented date to the console.
Example
function addDays(date, days) { // Function to add Days var result = new Date(date); result.setDate(result.getDate() + days); return result; } let date = new Date(); console.log("Current date is "+date); let incrementedDate = addDays(date, 7); console.log("Increment date is "+incrementedDate);
Output
Current date is Tue Mar 07 2023 11:38:38 GMT+0530 (India Standard Time) Increment date is Tue Mar 14 2023 11:38:38 GMT+0530 (India Standard Time)
Using setDate() and getDate() function
The getDate function returns the integer number between 1 to 31 which signifies the day of the month. The set date function is then used to set the value of the variable as the incremented date. Here we will be adding 14 days to the current date and displaying the result to the console. The incremented days are saved to the same variable “date” and hence another variable is not required.
Example
let date = new Date(); console.log("Current date is "+date); date.setDate(date.getDate()+14); console.log("Increment date is "+date);
Output
Current date is Tue Mar 07 2023 11:40:55 GMT+0530 (India Standard Time) Increment date is Tue Mar 21 2023 11:40:55 GMT+0530 (India Standard Time)
User defined function
Here we will be creating our own function instead of using any in built function to increment the date. We first extract the day integer of the month, the month and the year from the given date and store them as variables d, m and y respectively. We then add days to the d or day variable and then convert this back to a Date format while returning. This function has limited capabilities at the moment for adding days more than 1 month but can be modified or avoided at best as there are in built functions present.
Example
function AddDays(start,days){ var d=start.getDate(); var m=start.getMonth()+1; //getMonth returns the index of the month hence +1 is added var y=start.getYear(); //getYear returns the year minus 1900 in the current javascript version, hence 1900 is added to it below var newDate=m+"-"+(d+days)+"-"+(y+1900); return new Date(newDate); } today=new Date(); console.log("The date today is "+today); console.log("The date after 5 days is "+AddDays(today,5));
Output
The date today is Tue Mar 07 2023 11:43:02 GMT+0530 (India Standard Time) The date after 5 days is Sun Mar 12 2023 00:00:00 GMT+0530 (India Standard Time)
Add working days only
One might often see the increment of days being used on e-commerce websites to show the estimated delivery time. This delivery time is very often not available on Sundays. While calculating the estimated delivery date, Sunday must be excluded. It is also possible to exclude public holidays.
Here will be seeing how to add 7 working days to the current date.
Example
function AddWorkingDays(start,days){ // retrieve the index of the start date var d=start.getDay(); var incrementby=days; if(d==0){ // 0 stands for Sunday, if current day is a Sunday then add 1 day incrementby++; } if (d + incrementby >= 6) { //Subtract days in current working week from working days var remainingWorkingDays = incrementby - (5 - d); //Add current working week's weekend incrementby += 2; if (remainingWorkingDays > 5) { //Add two days for every working week by finding out how many weeks are included incrementby += 2 * Math.floor(remainingWorkingDays / 5); //Exclude the final weekend if the remainingWorkingDays is a equal to an exact number of weeks if (remainingWorkingDays % 5 == 0) incrementby -= 2; } } start.setDate(start.getDate() + incrementby); return start; } var today=new Date(); console.log("Current date is "+today); console.log("8 working days later would be "+AddWorkingDays(today,8));
Output
Current date is Tue Mar 07 2023 11:45:58 GMT+0530 (India Standard Time) 8 working days later would be Fri Mar 17 2023 11:45:58 GMT+0530 (India Standard Time)
Conclusion
All the above methods will allow you to add days, months, or even years to a given date. The Add Working days function is of great use in the industry and can be used by e-commerce platforms, delivery websites etc. Besides these methods, we can make use of the Moment.js library, however this would add an unnecessary complication to the program.