Add Seconds to a Date using JavaScript


Borislav Hadzhiev

Last updated: Jan 21, 2022


Photo from Unsplash

Add Seconds to a Date using JavaScript #

To add seconds to a date:

  1. Use the getSeconds() method to get the seconds of the specific date.
  2. Use the setSeconds() method to set the seconds for the date.
  3. The setSeconds method takes the seconds as a parameter and sets the value for the date.
function addSeconds(numOfSeconds, date = new Date()) { date.setSeconds(date.getSeconds() + numOfSeconds); return date; } // 👇️ Add 10 seconds to current Date const result = addSeconds(10); // 👇️ Add 20 seconds to another Date const date = new Date('2022-03-14T09:55:30.820'); // 👇️ Mon Mar 14 2022 09:55:50 console.log(addSeconds(20, date));

We created a reusable function that takes the number of seconds and a Date object and adds the seconds to the date.

If no Date object is provided to the function, it uses the current date.

The getSeconds() method returns a number between 0 and 59 that represents the seconds in the given date.

The setSeconds() takes a number representing the seconds as a parameter and sets the value on the date.

The JavaScript Date object automatically takes care of rolling over the minutes, hours, days, months and years if adding X seconds to the date changes their values.

const date = new Date('2022-03-14T09:55:30.820'); // 👇️ Add 121 seconds to Date date.setSeconds(date.getSeconds() + 121); // 👇️ Mon Mar 14 2022 09:57:31 console.log(date);

We added 121 seconds to the date, so the minutes had to be adjusted.

The Date object would automatically take care of adjusting the hour, day, month and year as well.

Note that the setSeconds method mutates the Date object it was called on. If you don't want to change the Date in place, you can create a copy of it before calling the method.
function addSeconds(numOfSeconds, date = new Date()) { const dateCopy = new Date(date.getTime()); dateCopy.setSeconds(dateCopy.getSeconds() + numOfSeconds); return dateCopy; } const date = new Date('2022-03-14T09:55:30.820'); const result = addSeconds(10, date); console.log(result); // 👉️ Mon Mar 14 2022 09:55:40 console.log(date); // 👉️ Mon Mar 14 2022 09:55:30 (didn't change original)

The getTime method returns the number of milliseconds elapsed between 1st of January, 1970 00:00:00 and the given date.

We used the timestamp to create a copy of the Date object, so we don't mutate it in place when calling the setSeconds method.

Copying the date is quite useful when you have to use the original Date object in other places in your code.

In general, mutating function arguments is a bad practice, as passing the same parameter to the same function multiple times returns different results.

You might see the setSeconds method get called with 2 parameters. The parameters the method takes are:

  1. seconds - a number representing the seconds.
  2. milliseconds (optional) - a number between 0 and 999 that represents the milliseconds.

If the milliseconds parameter is not provided, the value returned from the getMilliseconds() method is used.

For example, you could pass a second parameter to the setSeconds method if you want to set the milliseconds for the date to 0.
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.