How can I add 10 seconds to a JavaScript date object?
Something like this:
var timeObject = new Date() var seconds = timeObject.getSeconds() + 10; timeObject = timeObject + seconds;
Add x Seconds to a Date We can add x number of seconds to a Date instance by using the getSeconds method to get the number of seconds in the date object. For instance, we can write: const date = new Date('2020-01-01 10:11:55'); date.
const date = new Date(); date. setDate(date. getDate() + 1); // ✅ 1 Day added console.
There are 60 seconds in a minute, 60 minutes in an hour, and 24 hours in a day. Therefore, one day is: 1000 * 60 * 60 * 24 , which is 86400000 milliseconds. Date.
getTime() The getTime() method returns the number of milliseconds since the ECMAScript epoch. You can use this method to help assign a date and time to another Date object.
There's a setSeconds
method as well:
var t = new Date(); t.setSeconds(t.getSeconds() + 10);
For a list of the other Date
functions, you should check out MDN
setSeconds
will correctly handle wrap-around cases:
var d; d = new Date('2014-01-01 10:11:55'); alert(d.getMinutes() + ':' + d.getSeconds()); //11:55 d.setSeconds(d.getSeconds() + 10); alert(d.getMinutes() + ':0' + d.getSeconds()); //12:05
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With