Skip to content
Advertisement

Add 10 seconds to a Date

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;

Advertisement

Answer

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
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement