solvit.io

Add a given number of seconds to a JavaScript date

Solution
var dateObject = ...
var numberOfSecondsToAdd = ...

dateObject.setSeconds(dateObject.getSeconds() + numberOfSecondsToAdd);

// if you need millisecond precision
dateObject.setSeconds(
    dateObject.getSeconds() + numberOfSecondsToAdd,
    numberOfMillisecondsToAdd
);

If the resulting number of seconds is more than 60, the rest of the date is updated accordingly to add minutes, hours, etc.

Example
var now = new Date();
var tenMinutesInSeconds = 600;

// if 'now' = 201X-01-01 09:05:23
now.setSeconds(now.getSeconds() + tenMinutesInSeconds);
// here 'now' = 201X-01-01 09:15:23
Find more solutions about: , ,