The JavaScript setSeconds function is a date method used to set the seconds and milliseconds of a given date according to the local system time zone.
The syntax of the setSeconds function is:
Date.setSeconds(Seconds)
Date.setSeconds(Seconds, Milliseconds)
Parameter: The JavaScript setSeconds function accepts seconds and milliseconds (optional) values, where seconds range between 0 and 59 and milliseconds between 0 and 999 to pass it to the original date.
If you pass a seconds value above 59 or above (for Example, 90), it considers 60 seconds as 1 minute and adds 1 minute to the existing date, setting the remaining 30 as the seconds value.
Return Value: The setSeconds() method returns a new and updated date with the given seconds and milliseconds. If the parameter value is NaN or undefined, the date becomes invalid, and the setSeconds() result is NaN.
JavaScript setSeconds example
In the following example, we use the setSeconds() method to set the seconds value to 10 of the current date and time.
NOTE: In this setSeconds function, the milliseconds argument is optional. Either you can use it or avoid it.
let dt = new Date();
console.log(dt);
dt.setSeconds(10);
console.log(dt);
Mon Jun 15 2026 09:28:15 GMT+0530 (India Standard Time)
Mon Jun 15 2026 09:28:10 GMT+0530 (India Standard Time)
Example 2: In this JavaScript example, we use the Date() constructor to parse a string date and use setSeconds() to set the Seconds of a custom date to 55.
let dt = new Date("January 1, 2017 10:11:22");
dt.setSeconds(55);
console.log(dt);
Sun Jan 01 2017 10:11:55 GMT+0530 (India Standard Time)
Example 3: If we use the setSeconds() method on a custom date without a time value, it considers 0 as the default seconds value and updates it by the given number of seconds.
let dt = new Date("January 1, 2017");
console.log(dt);
dt.setSeconds(55);
console.log(dt);
Sun Jan 01 2017 00:00:00 GMT+0530 (India Standard Time)
Sun Jan 01 2017 00:00:55 GMT+0530 (India Standard Time)
Example 4: In this example, we use the JavaScript setSeconds() method to set seconds and milliseconds of the current date and time. Here, we used two parameters (seconds as 55 and milliseconds as 777).
NOTE: We can use the getSeconds() and getMilliseconds() methods to extract seconds and milliseconds from the date.
let dt = new Date();
console.log(dt.toISOString());
dt.setSeconds(55, 777);
console.log(dt.toISOString());
console.log(dt.getSeconds());
console.log(dt.getMilliseconds());
2026-06-15T04:05:01.978Z
2026-06-15T04:05:55.777Z
55
777
Example 5: As we mentioned earlier, if we pass a seconds value greater than 59, it updates the minutes or hours value based on the number of seconds.
Here, 1000 seconds means 16 Minutes and 40 seconds. So, the setSeconds () method adds 16 minutes to the current time and sets 40 seconds as the seconds value.
let dt = new Date();
console.log(dt);
dt.setSeconds(1000);
console.log(dt);
Mon Jun 15 2026 09:38:51 GMT+0530 (India Standard Time)
Mon Jun 15 2026 09:54:40 GMT+0530 (India Standard Time)
Example 6: Using JavaScript setSeconds() with negative seconds
If we use the setSeconds() function with a negative seconds value, it sets the time to the previous minute’s last second. Here, the time is 09:42:32, and the previous minutes last second is 42:59. So, the result is 09:42:59.
let dt = new Date();
console.log(dt);
dt.setSeconds(-1);
console.log(dt);
Mon Jun 15 2026 09:43:32 GMT+0530 (India Standard Time)
Mon Jun 15 2026 09:42:59 GMT+0530 (India Standard Time)
How to add 10 seconds to a Date?
We can use the setSeconds() and getSeconds() methods to add 10 seconds to a current date or a custom datetime.
let dt = new Date();
console.log(dt);
dt.setSeconds(dt.getSeconds() + 10);
console.log(dt);
Mon Jun 15 2026 09:42:51 GMT+0530 (India Standard Time)
Mon Jun 15 2026 09:43:01 GMT+0530 (India Standard Time)
The other approach, and the most reliable one, uses the getTime() function with the arithmetic + operator. Here, 10 * 1000 means total milliseconds. If you want to add 100 seconds, replace 10 with 100.
let dt = new Date();
console.log(dt);
const newDate = new Date(dt.getTime() + 10 * 1000);
console.log(newDate);
Mon Jun 15 2026 09:47:53 GMT+0530 (India Standard Time)
Mon Jun 15 2026 09:48:03 GMT+0530 (India Standard Time)