Subtract Seconds from a Date in JavaScript

avatar

Borislav Hadzhiev

Last updated: Jan 21, 2022

banner

Photo from Unsplash

Subtract Seconds from a Date in JavaScript #

To subtract seconds from 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.
index.js
function subtractSeconds(numOfSeconds, date = new Date()) { date.setSeconds(date.getSeconds() - numOfSeconds); return date; } // 👇️ Subtract 10 seconds from current Date const result = subtractSeconds(10); // 👇️ Subtract 20 seconds from another date const date = new Date('2022-06-24T07:35:40.820'); // 👇️ Fri Jun 24 2022 07:35:20 console.log(subtractSeconds(20, date));

We created a reusable function that takes the number of seconds and a Date object and subtracts the seconds from 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 adjusting the minutes, hours, days, months and years if subtracting X seconds from the date changes their values.

index.js
const date = new Date('2022-06-24T00:00:40.820'); date.setSeconds(date.getSeconds() - 50); console.log(date); // 👉️ Thu Jun 23 2022 23:59:50

The example above shows how subtracting 50 seconds from the date automatically adjusts the values for the day of the month, hour, minutes and seconds on the Date object.

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.
index.js
function subtractSeconds(numOfSeconds, date = new Date()) { const dateCopy = new Date(date.getTime()); dateCopy.setSeconds(dateCopy.getSeconds() - numOfSeconds); return dateCopy; } const date = new Date('2022-06-24T06:30:40.820'); const result = subtractSeconds(25, date); console.log(result); // 👉️ Fri Jun 24 2022 06:30:15 console.log(date); // 👉️ Fri Jun 24 2022 06:30:40 (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.
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.