We can use JavaScript to add seconds to a date by using the JavaScript getTime() method. The getTime() method converts dates into the number of milliseconds since January 1st, 1970. Since there are 1000 milliseconds in 1 second, we can add multiples of 1000 to our converted date to add as many seconds as we want.
var date1 = new Date();
var dateToMilliseconds = date1.getTime();
var addedSeconds = dateToMilliseconds + (1000*30);
//This will add 30 seconds to our time.
var newDate = new Date(addedSeconds);
//This will create a new date that will be 30 seconds ahead of the current date
We can also add negative milliseconds to subtract seconds from a date. Below are some examples of adding seconds 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+(1000*60));
var result3 = new Date(dateToMilliseconds+(1000*60*60));
var result4 = new Date(dateToMilliseconds+(1000*60*60*24));
var result5 = new Date(dateToMilliseconds+(-1000*60));
Which would result in the following:
result2 = Fri Jun 24 2022 11:48:46 GMT-0500 (Central Daylight Time)
result3 = Fri Jun 24 2022 12:47:46 GMT-0500 (Central Daylight Time)
result4 = Sat Jun 25 2022 11:47:46 GMT-0500 (Central Daylight Time)
result5 = Fri Jun 24 2022 11:46:46 GMT-0500 (Central Daylight Time)
Add Seconds to a Date Using JavaScript with a Click
In this example, we will let the user add seconds 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 seconds do you want to add to the current date and time?</p>
<input id="userVal" type="text" value="">
<input id="submitNum" type="submit" onclick="addSeconds()" 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 seconds to the current date like we did above. We will then update the #results div using the textContent property.
function addSeconds() {
var date1 = new Date();
var dateToMilliseconds = date1.getTime();
var userSeconds = document.getElementById("userVal").value;
var addedSeconds = dateToMilliseconds + (1000*userSeconds);
var newDate = new Date(addedSeconds);
document.getElementById("results").textContent = newDate;
}
The final code and output for this example is below:
Code Output:
How many seconds do you want to add to the current date and time?
Full Code:
<p>How many seconds do you want to add to the current date and time?</p>
<input id="userVal" type="text" value="">
<input id="submitNum" type="submit" onclick="addSeconds()" value="Submit">
<div id="results"></div>
<script>
function addSeconds() {
var date1 = new Date();
var dateToMilliseconds = date1.getTime();
var userSeconds = document.getElementById("userVal").value;
var addedSeconds = dateToMilliseconds + (1000*userSeconds);
var newDate = new Date(addedSeconds);
document.getElementById("results").textContent = newDate;
}
</script>
Hopefully this article has been useful in helping you understand how to add seconds to a date using JavaScript.
Leave a Reply