To add days to a date, we can use the JavaScript getTime() method. The getTime() method converts dates into the number of milliseconds since January 1st, 1970. Since there are 86400000 milliseconds in 1 day, we can add multiples of 86400000 to our converted date to add as many days as we want.
var date1 = new Date();
var dateToMilliseconds = date1.getTime();
var addedDays = dateToMilliseconds + (86400000*5);
//This will add 5 days to our date.
var newDate = new Date(addedDays);
//This will create a new date that will be 5 days ahead of the current date
We can also add negative milliseconds to subtract days from a date. Below are some examples of adding days to a date object with the help of the getTime() method.
var date1 = new Date();
var dateToMilliseconds = date1.getTime();
var result1 = date1;
var result2 = new Date(dateToMilliseconds+(86400000*5));
var result3 = new Date(dateToMilliseconds+(86400000*7));
var result4 = new Date(dateToMilliseconds+(86400000*30));
var result5 = new Date(dateToMilliseconds+(-86400000*5));
Which would result in the following:
result2 = Wed Jan 26 2022 15:14:52 GMT-0500 (Eastern Standard Time);
result3 = Fri Jan 28 2022 15:14:52 GMT-0500 (Eastern Standard Time);
result4 = Sun Feb 20 2022 15:14:52 GMT-0500 (Eastern Standard Time);
result5 = Sun Jan 16 2022 15:14:52 GMT-0500 (Eastern Standard Time);
Add Days to a Date Using JavaScript with a Click
In this example, we will let the user add days to the current date using an input field. We will use the getTime() method to convert dates to milliseconds like we did above.
Here is our HTML setup:
<p>How many days do you want to add to the current date and time?</p>
<input id="userVal" type="text" value="">
<input id="submitNum" type="submit" onclick="addDays()" value="Submit">
<div id="results"></div>
Below is the JavaScript code which will take the user input using the onclick event and run the function below. The function will add the days to the current date like we did above. We will then update the #results div using the textContent property.
function addDays() {
var date1 = new Date();
var dateToMilliseconds = date1.getTime();
var userDays = document.getElementById("userVal").value;
var addedDays = dateToMilliseconds + (86400000*userDays);
var newDate = new Date(addedDays);
document.getElementById("results").textContent = newDate;
}
The final code and output for this example is below:
Code Output:
How many days do you want to add to the current date and time?
Full Code:
<p>How many days do you want to add to the current date and time?</p>
<input id="userVal" type="text" value="">
<input id="submitNum" type="submit" onclick="addDays()" value="Submit">
<div id="results"></div>
<script>
function addDays() {
var date1 = new Date();
var dateToMilliseconds = date1.getTime();
var userDays = document.getElementById("userVal").value;
var addedDays = dateToMilliseconds + (86400000*userDays);
var newDate = new Date(addedDays);
document.getElementById("results").textContent = newDate;
}
</script>
Hopefully this article has been useful in helping you understand how to Add Days to a Date Using JavaScript.
Leave a Reply