How to add 10 seconds to a JavaScript date object?


In this tutorial, we will learn how to add 10 seconds to a JavaScript Date object. Here we will discuss two methods which are following.

  • Using the setSeconds( ) Method

  • Using the getTime() Method

Using the setSeconds() Method

JavaScript date setSeconds() method sets the seconds for a specified date according to local time. This method takes two arguments, the first is the number of seconds between 0 to 59 and the second is the number of milliseconds between 0 to 999.

If you do not specify the second parameter, the value returned from the getMilliseconds() method is used. If a parameter you specify is outside of the expected range, setSeconds() method attempts to update the date information in the Date object accordingly.

For example, if you use the 100 for seconds value, the minutes stored in the Date object will be incremented by 1, and 40 will be used for seconds.

Syntax

Date.setSeconds(seconds, ms)

Parameter

  • seconds − It’s an integer between 0 and 59, representing the seconds. If you specify the seconds parameter, you must also specify the minutes.

  • ms − It’s a number between 0 and 999, representing the milliseconds. If you specify the ms parameter, you must also specify the minutes and seconds.

Approach

To add 10 seconds into the Date object using the setSeconds() method first we get the value of seconds of the current time and then add 10 into it and pass the added value to the setSeconds( ) method.

Example

In this example we are adding 10 seconds to the current time using the setSeconds( ) method.

<html> <head> <title>Example- adding 10 seconds to a JavaScript date object</title> </head> <body> <h3> Add 10 seconds to the JavaScript Date object using setSeconds( ) method </h3> <p> Click on the button to add 10 seconds to the current date/time.</p> <button onclick="add()">Click Me</button> <p id="currentTime">Current Time : </p> <p id="updatedTime">Updated Time: </p> </body> <script> // Code the show current time let ct = document.getElementById("currentTime") setInterval(() => { let currentTime = new Date().getTime(); ct.innerText = "Current Time : " + new Date(currentTime).toLocaleTimeString() }, 1000) // Code to add 10 seconds to current Time let ut = document.getElementById("updatedTime") function add() { setInterval(() => { let dt = new Date(); dt.setSeconds(dt.getSeconds() + 10); ut.innerText = "Updated Time : " + dt.toLocaleTimeString(); }, 1000) } </script> </html>

Using the getTime( ) Method

JavaScript date getTime() method returns the numeric value corresponding to the time for the specified date according to universal time. The value returned by the getTime() method is the number of milliseconds since 1 January 1970 at 00:00:00.

Syntax

Date.getTime()

Approach

To add 10 seconds to the Date object, first, we get the current time by using the Date.getTime( ) method and then add 10000 milliseconds to it and pass the added value to the Date Object.

Example

In this example, we are adding 10 seconds to the current time using the getTime() method.

<html> <head> <title>Example – adding 10 seconds to Date object</title> </head> <body> <h3> Add 10 seconds to the JavaScript Date object </h3> <p> Click on the button to add 10 seconds to the current date/time.</p> <button onclick="add()">Click Me</button> <p id="currentTime">Current Time : </p> <p id="updatedTime">Updated Time: </p> </body> <script> // Code the show current time let ct = document.getElementById("currentTime") setInterval(() => { let currentTime = new Date().getTime(); ct.innerText = "Current Time : " + new Date(currentTime).toLocaleTimeString() }, 1000) // Code to add 10 seconds to current Time let ut = document.getElementById("updatedTime") function add() { setInterval(() => { let currentTime = new Date().getTime(); let updatedTIme = new Date(currentTime + 10000); ut.innerText = "Updated Time : " + updatedTIme.toLocaleTimeString() }, 1000) } </script> </html>

In summary, we have discussed two methods to add 10 seconds to a JavaScript date object. The first method is to use the setSeconds() method and the second is to use the getTime() method.

Updated on: 22-Aug-2022

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements