JavaScript Date setSeconds() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Date

Definition

Date.prototype.setSeconds() changes the seconds and/or milliseconds of a date according to local time (MDN). It mutates the Date in place and returns the new timestamp.

Remember
date.setSeconds(seconds)
date.setSeconds(seconds, ms)

seconds: 0 … 59   (local)
ms:      0 … 999  (optional)
→ returns new timestamp (ms)
→ mutates date

setSeconds(42)        // keep current ms
setSeconds(0, 0)      // snap to :00.000
setSeconds(100)       // overflow → +1 minute, :40

Syntax

Call date.setSeconds. Start with this line.

JavaScript
date.setSeconds(s[, ms])
CallWhat it does
date.setSeconds(s[, ms])Primary signature.
date.setSeconds(...)Call with the arguments your case needs.

Built into JavaScript. Call setSeconds(...) on a Date instance — no import required.

Library declarations
JavaScript
date.setSeconds(s[, ms])

Parameters

ParameterDescription
secondsValueRequired. Local seconds (typically 0–59; overflow allowed)
msValueOptional. Local milliseconds; keep current if omitted

Return Value

Return ValueDescription
*New epoch ms (number), not the Date

JavaScript Examples

Example 1 — Set the Seconds Only

MDN-style: change the seconds and keep the existing minutes.

JavaScript
const event = new Date(1975, 7, 19, 23, 15, 30);

event.setSeconds(42);

console.log(event.getSeconds()); // 42
console.log(event.getMinutes()); // 15
Try It Yourself

How It Works

  1. Start at 23:15:30. Month index 7 is August.
  2. Call setSeconds(42). Only the seconds argument is provided.
  3. Minutes stay. Minutes remain 15; ms keep their current value.
  4. Object mutated. The same event now reads seconds 42.

Example 2 — Set Seconds and Milliseconds Together

Pass both arguments to snap to an exact local second within the minute.

JavaScript
const event = new Date(1975, 7, 19, 23, 15, 30, 500);

event.setSeconds(0, 0);

console.log(event.getSeconds());      // 0
console.log(event.getMilliseconds()); // 0
Try It Yourself

How It Works

  1. Start with leftover ms. The date has 500 milliseconds.
  2. Pass two numbers. setSeconds(0, 0) sets second and ms.
  3. Snap cleanly. Result is :00.000 within the same minute.
  4. Why it helps. One call clears leftover sub-seconds without a separate setMilliseconds().

Example 3 — Second Overflow Rolls to the Next Minute

Values outside 0–59 normalize into later minutes (MDN).

JavaScript
const d = new Date(2026, 0, 1, 12, 30, 45); // Jan 1, 2026 12:30:45

d.setSeconds(100);

console.log(d.getMinutes()); // 31
console.log(d.getSeconds()); // 40
Try It Yourself

How It Works

  1. Start at 12:30:45. Local minute is 30.
  2. Ask for second 100. That is past the end of the minute.
  3. Engine normalizes. 100 = 60 + 40 → add 1 minute, keep second 40.
  4. Read back. Minutes become 31; seconds become 40.

Browser & Runtime Support

Date.prototype.setSeconds() is part of the language from early ECMAScript and is Baseline Widely available (MDN).

✓ Baseline · Widely available

Date.prototype.setSeconds()

Supported in every modern browser and Node.js. No polyfill needed for ordinary web apps.

99% Modern browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
Date.setSeconds() Excellent

Bottom line: Safe everywhere. Focus on mutation, overflow into minutes, and DST when adjusting local seconds.

Frequently Asked Questions

It sets the seconds (0–59) of a Date in local time. An optional milliseconds argument can also set sub-second precision. The Date mutates in place and the method returns the new timestamp in milliseconds.
getSeconds() reads the local second. setSeconds(s) writes a new local second. They are the read/write pair for the second hand.
Yes. setSeconds(second, ms) updates both at once — useful for snapping to :00.000 at the start of a minute.
JavaScript normalizes overflow into the next minute. setSeconds(100) becomes +1 minute and 40 seconds (MDN).
No. It returns a number (epoch milliseconds). Call setters on the Date variable — do not chain on the return value.
setSeconds() changes the local clock second. setUTCSeconds() changes the UTC clock second. For fixed elapsed-time math across DST, prefer setUTCSeconds() or setTime().

Next: setTime()

Learn how to set a Date from a full epoch milliseconds timestamp.

setTime() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful