Date.prototype.getSeconds()

Return the seconds past the minute for a date according to local time.

Signature
getSeconds()
  • Added in JavaScript ES1

Return value

numberAn integer between 0 and 59 representing the seconds for the given date according to local time. Returns NaN if the date is invalid.

Errors

Throws a TypeError if called on a value that is not a Date object.

Returns the seconds past the minute, 0 through 59, in local time. It completes the HH:MM:SS triple with getHours() and getMinutes().

When to reach for it

Use it when rendering a clock or a timer face, when zeroing the sub-minute part of a timestamp before comparing two times, or when displaying a duration that has already been broken into components.

What trips people up

It is a component, not a duration. getSeconds() tells you where in the current minute the date sits — it is not "seconds since something". For elapsed time, subtract two timestamps and divide by 1000. Reading getSeconds() off a difference of two Dates is a common mistake that silently works while the gap is under a minute and then wraps.

The value truncates. A date at 10:00:45.900 reports 45, not 46, and the missing 900 ms live in getMilliseconds(). Two timestamps that display the same second can be nearly a full second apart, which makes equality checks on formatted times unreliable.

There is no leap second: the range stops at 59, because ECMAScript time values ignore them entirely.

Like the other component getters it returns NaN for an invalid date, and it needs zero-padding for display since 5 formats as "5" rather than "05".

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Render a live clock or stopwatch face as HH:MM:SS
  • Zero the seconds on a timestamp before comparing two times to the minute
  • Split a duration in seconds into minutes and remaining seconds for display
  • Check that a scheduled task fired within the expected second

Frequently asked questions

How do I get the elapsed seconds between two dates?

Subtract the two Dates and divide by 1000: (end - start) / 1000. Do not subtract their getSeconds() values, which wrap every minute and give negative or wrong results.

Why does getSeconds() return 30 when the time is 30.9 seconds?

The seconds field is a whole number and the fractional part lives in getMilliseconds(). Combine them as getSeconds() + getMilliseconds() / 1000 when you need sub-second precision.

Can getSeconds() return 60 for a leap second?

No — the range is 0 to 59. ECMAScript time values ignore leap seconds entirely, so they never appear in a Date.

More examples

Component versus duration

The real gap is 30 seconds. Subtracting the seconds fields gives -30 because the minute rolled over, and wrapping the difference in a Date is worse still since it is read in local time.

Example · javascript
const start = new Date(2024, 0, 1, 10, 0, 50);
const end   = new Date(2024, 0, 1, 10, 1, 20);

// The right way: subtract timestamps
console.log((end - start) / 1000);

// The wrong way: reading the seconds field
console.log(end.getSeconds() - start.getSeconds());
console.log(new Date(end - start).getSeconds());
console.log(end.getSeconds(), start.getSeconds());

Truncating to the minute

Two times in the same minute are different instants until the sub-minute fields are cleared. The second argument to setSeconds() zeroes the milliseconds, which is easy to forget.

Example · javascript
function toMinute(date) {
  const d = new Date(date);
  d.setSeconds(0, 0); // also clears milliseconds
  return d;
}

const a = new Date(2024, 0, 1, 9, 30, 12, 800);
const b = new Date(2024, 0, 1, 9, 30, 59, 100);

console.log(a.getTime() === b.getTime());
console.log(toMinute(a).getTime() === toMinute(b).getTime());
console.log(toMinute(a).getSeconds(), toMinute(a).getMilliseconds());

Formatting a duration

Plain arithmetic gives 1:30, 10:05 and 0:09. The final line shows why a Date cannot hold a duration: 90 seconds normalizes into 1 minute 30, and the field reports 30.

Example · javascript
function mmss(totalSeconds) {
  const m = Math.floor(totalSeconds / 60);
  const s = totalSeconds % 60;
  return `${m}:${String(s).padStart(2, "0")}`;
}

console.log(mmss(90));
console.log(mmss(605));
console.log(mmss(9));

// Reading it off a Date field instead wraps at 60
console.log(new Date(2024, 0, 1, 0, 0, 90).getSeconds());

Discussion

  • Be the first to comment on this lesson.