JavaScript Date setSeconds() Method

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Local second (0–59)

What You’ll Learn

setSeconds() writes the second (0–59) on a Date in local time, with an optional milliseconds argument. This guide covers syntax, minute-boundary snaps, overflow, and comparisons with UTC setters.

01

Syntax

setSeconds(s)

02

0–59

Seconds

03

+ ms

2nd arg

04

Mutates

In place

05

Overflow

Rolls minutes

06

Returns

Epoch ms

Introduction

Timestamps from logs, APIs, and cron jobs often need their second component adjusted. After reading seconds with getSeconds(), setSeconds() writes them back in local time.

Like other setters, it mutates the original object and returns epoch milliseconds—not the Date for chaining.

Understanding the setSeconds() Method

Date.prototype.setSeconds(second [, millisecond]) accepts a local second and an optional millisecond (0–999). When you pass only the second, minutes and hours stay the same unless overflow normalization rolls the clock forward.

Values outside normal ranges normalize— setSeconds(90) becomes 1 minute and 30 seconds later. For UTC second writes, use setUTCSeconds().

💡
Beginner Tip

To snap to the start of a minute: date.setSeconds(0, 0). For exact :45.500 marks: date.setSeconds(45, 500).

📝 Syntax

JavaScript
dateObj.setSeconds(second)
dateObj.setSeconds(second, millisecond)

Parameters

  • second — Integer 0–59 in local time (overflow normalizes).
  • millisecond (optional) — 0–999.

Return value

  • Updated epoch milliseconds after the change.
  • NaN if the Date was invalid before the call.

⚡ Quick Reference

GoalCode
Set second onlydate.setSeconds(30)
Start of minutedate.setSeconds(0, 0)
Set s + ms togetherdate.setSeconds(45, 500)
Read second after setdate.getSeconds()
Elapsed durationend.getTime() - start.getTime()
Clone before mutateconst copy = new Date(date)

📋 setSeconds() vs Similar Methods

Pick the setter that matches the time field and timezone you need.

setSeconds()
local 0–59

Local second

getSeconds()
read

Getter pair

setMinutes()
minutes

Minute setter

setUTCSeconds()
UTC 0–59

UTC setter

Examples Gallery

Open DevTools Console (F12) or use Try-it links. Examples use the Date constructor for predictable local times.

📚 Getting Started

Change only the second while keeping minutes and hours.

Example 1 — Set the Second to 30 (Keep Minutes and Hours)

Start at 12:15:05 and move the second hand to 30.

JavaScript
const date = new Date(2026, 2, 1, 12, 15, 5);
date.setSeconds(30);

console.log(date.getMinutes());  // 15
console.log(date.getSeconds());  // 30
Try It Yourself

How It Works

Passing one argument updates only the second. Minutes and hours remain unless normalization rolls the clock.

📈 Practical Patterns

Minute snaps, combined second/ms, cloning, and overflow.

Example 2 — Snap to Start of Minute (setSeconds(0, 0))

Clear seconds and milliseconds while keeping hour and minute.

JavaScript
const date = new Date(2026, 2, 1, 12, 15, 42, 750);
date.setSeconds(0, 0);

console.log(date.getMinutes());       // 15
console.log(date.getSeconds());       // 0
console.log(date.getMilliseconds());  // 0
Try It Yourself

How It Works

Useful for minute-granularity comparisons or grouping log entries by minute.

Example 3 — Set Seconds and Milliseconds Together

Pin an exact sub-minute timestamp with one call.

JavaScript
const date = new Date(2026, 2, 1, 9, 0, 0, 0);
date.setSeconds(45, 500);

console.log(date.getSeconds());       // 45
console.log(date.getMilliseconds());  // 500
Try It Yourself

How It Works

The two-argument form sets both the second and millisecond fields at once.

Example 4 — Clone, Then Set (Immutable Style)

Preserve the original Date by copying before mutating.

JavaScript
const original = new Date(2026, 2, 1, 12, 15, 10);
const adjusted = new Date(original);
adjusted.setSeconds(55);

console.log("Original s:", original.getSeconds());  // 10
console.log("Adjusted s:", adjusted.getSeconds());  // 55
Try It Yourself

How It Works

Setters always mutate their target. Clone first when other code still needs the old instant.

Example 5 — Second Overflow (setSeconds(90))

Passing 90 rolls into the next minute at second 30.

JavaScript
const date = new Date(2026, 2, 1, 12, 30, 45);
date.setSeconds(90);

console.log(date.getMinutes());  // 31
console.log(date.getSeconds());  // 30
Try It Yourself

How It Works

90 seconds = 1 minute + 30 seconds. The engine normalizes instead of clamping to 59.

🚀 Common Use Cases

  • Log alignmentsetSeconds(0, 0) for minute buckets.
  • API timestamps — apply second/ms from parsed ISO strings.
  • Test fixtures — pin exact second/ms in unit tests.
  • Cron alignment — snap events to :00 at minute boundaries.
  • Form time pickers — apply user-selected seconds to a Date.
  • Clone + mutate — keep originals when tweaking sub-minute fields.

🧠 How setSeconds() Updates the Clock

1

Read local time

Engine loads hour, minute, second, and ms fields.

Local
2

Apply new second

Second and optional ms replace old sub-minute values.

setSeconds
3

Normalize overflow

Out-of-range values roll into adjacent minutes or hours.

Normalize
4

Mutate & return ms

Date updates in place; epoch milliseconds are returned.

📝 Notes

  • setSeconds() uses local time seconds 0–59 (overflow normalizes).
  • For elapsed duration, use end.getTime() - start.getTime()—not clock getters.
  • Mutates the original Date—clone with new Date(date) when needed.
  • Returns epoch ms, not the Date—no fluent chaining on the return value.
  • Optional millisecond defaults to 0 when omitted in two-arg calls.
  • Use setUTCSeconds when rules follow UTC, not local wall clock.

Browser & Runtime Support

Date.prototype.setSeconds() has been available since ES1. It works in every browser and Node.js.

Baseline · ES1

Date.prototype.setSeconds()

Supported in Chrome, Firefox, Safari, Edge, Internet Explorer, and all Node.js versions. No polyfill required.

99% Universal API
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. Remember mutation, overflow, and getTime() for duration.

Conclusion

setSeconds() writes the local second (and optionally milliseconds) on a JavaScript Date. Use it for minute snaps and precise sub-minute timestamps; clone first when preserving the original instant.

Next, learn setTime() to set the full epoch timestamp at once, or review getSeconds() for reading the second component.

💡 Best Practices

✅ Do

  • Use setSeconds(0, 0) to snap to minute boundaries
  • Use setSeconds(45, 500) for exact sub-minute times
  • Clone before mutating shared Date instances
  • Pair with getSeconds() to verify results
  • Use getTime() for elapsed measurements

❌ Don’t

  • Chain setters on the numeric return value
  • Assume values clamp at 59 (overflow rolls minutes)
  • Use setSeconds(n) to mean “n seconds elapsed”
  • Mix local setters with UTC getters in one label
  • Rely on manual 0–59 validation when overflow is acceptable

Key Takeaways

Knowledge Unlocked

Five things to remember about Date.setSeconds()

Your foundation for local second writes in JavaScript.

5
Core concepts
🔢 02

0–59

Seconds.

Range
📈 03

2 args

s, ms.

Overload
04

Overflow

Rolls minutes.

Normalize
🔄 05

Clone

Keep original.

Pattern

❓ Frequently Asked Questions

It sets the seconds (0–59) of a Date object in local time. An optional milliseconds argument can also set sub-second precision. The Date mutates in place and returns updated epoch milliseconds.
getSeconds() reads the current local second. setSeconds(s) writes a new second. They are the read/write pair for the second hand of the local clock.
Yes. setSeconds(second, millisecond) updates both fields at once—useful for snapping to :00.000 at the start of a minute.
JavaScript normalizes overflow into the next minute. setSeconds(90) on 12:30:45 becomes 12:31:30 (90 seconds = 1 minute + 30 seconds).
No. It returns a number (epoch milliseconds). Call each setter on the same Date variable—do not chain on the return value.
setSeconds() changes the local clock second. setUTCSeconds() changes the UTC clock second. Use the one that matches your timezone rules.
Did you know?

setSeconds(15) sets the clock’s second hand to 15—it does not mean “15 seconds from now.” For elapsed time, subtract Date.now() - start or use getTime().

Continue to setTime()

Learn how to set the full epoch milliseconds timestamp on a Date object.

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.

6 people found this page helpful