JavaScript Date getSeconds() Method

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

What You’ll Learn

getSeconds() returns the second component as an integer from 0 to 59 in local time. This guide covers syntax, five examples, UTC comparisons, validation, clock formatting, and common pitfalls with timers.

01

Syntax

date.getSeconds()

02

Range

0 – 59

03

Local time

Not UTC

04

HH:MM:SS

Clock format

05

Not elapsed

vs getTime

06

Validate

Invalid → NaN

Introduction

Stopwatch displays, log timestamps, and “run at :30” rules often need the second part of a Date. The getSeconds() method returns that value as an integer using the user’s local timezone.

Pair it with getHours() and getMinutes() to build HH:MM:SS strings, or use toLocaleTimeString() for locale-aware output.

Understanding the getSeconds() Method

Date.prototype.getSeconds() is a zero-argument instance method. It never mutates the original Date—it only reads the second component within the current minute in local time.

On a time like 14:30:45, getSeconds() returns 45. Sub-second precision comes from getMilliseconds().

💡
Beginner Tip

getSeconds() reads the clock second (0–59), not elapsed seconds on a timer. For duration, use Date.now() - start or getTime().

📝 Syntax

JavaScript
dateObj.getSeconds()

Parameters

  • None.

Return value

  • Integer 0–59 for a valid date in local time.
  • NaN if dateObj is an invalid Date.

⚡ Quick Reference

GoalCode
Current secondsnew Date().getSeconds()
Seconds from fixed timenew Date(2026, 6, 4, 14, 30, 45).getSeconds() → 45
UTC secondsdate.getUTCSeconds()
Pad for clockString(s).padStart(2, "0")
Elapsed durationDate.now() - start
Valid date check!Number.isNaN(date.getTime())

📋 getSeconds() vs Similar Methods

These getters split one instant into time parts. Pick the method that matches your task.

getSeconds()
0–59

Local seconds

getUTCSeconds()
UTC sec

Timezone-safe read

getMilliseconds()
0–999

Sub-second

getTime()
epoch ms

Full instant

Examples Gallery

Open DevTools Console (F12) or use Try-it links. Fixed times use new Date(y, m, d, h, min, sec) for predictable local values.

📚 Getting Started

Read the second component from the current time.

Example 1 — Get the Current Seconds

Call getSeconds() on a new Date() to read the second right now.

JavaScript
const now = new Date();
const seconds = now.getSeconds();

console.log("Current seconds:", seconds);
Try It Yourself

How It Works

The value updates every second and stays between 0 and 59. Minutes and hours are separate getters.

📈 Practical Patterns

Fixed times, validation, display, and minute-half logic.

Example 2 — Read Seconds from a Specific Time

Build a date at 14:30:45 local time and read its second component.

JavaScript
const stamp = new Date(2026, 6, 4, 14, 30, 45);

console.log(stamp.getMinutes());  // 30
console.log(stamp.getSeconds());  // 45
Try It Yourself

How It Works

The six-argument constructor sets local year through seconds. The last numeric argument before milliseconds is the second value.

Example 3 — Validate Before Calling getSeconds()

Guard against invalid Date objects that yield NaN.

JavaScript
function safeGetSeconds(date) {
  if (!(date instanceof Date) || Number.isNaN(date.getTime())) {
    return null;
  }
  return date.getSeconds();
}

console.log(safeGetSeconds(new Date()));              // e.g. 22
console.log(safeGetSeconds(new Date("not-a-date")));  // null
Try It Yourself

How It Works

getTime() returns NaN on invalid dates. Checking first prevents broken clock displays and bad comparisons.

Example 4 — Build an HH:MM:SS Clock String

Combine hour, minute, and padded second getters for a digital clock.

JavaScript
function formatHms(date) {
  const h = date.getHours();
  const m = String(date.getMinutes()).padStart(2, "0");
  const s = String(date.getSeconds()).padStart(2, "0");
  return h + ":" + m + ":" + s;
}

console.log(formatHms(new Date(2026, 6, 4, 9, 5, 7)));
// "9:05:07"
Try It Yourself

How It Works

padStart(2, "0") on minutes and seconds keeps fixed-width segments. For production, prefer toLocaleTimeString() or Intl.DateTimeFormat.

Example 5 — First or Second Half of the Minute

Branch on whether the current second is before or after the 30-second mark.

JavaScript
function minuteHalf() {
  const sec = new Date().getSeconds();

  if (sec < 30) {
    return "First half of the minute.";
  }
  return "Second half of the minute.";
}

console.log(minuteHalf());
Try It Yourself

How It Works

Seconds 0–29 are the first half; 30–59 are the second. This pattern suits polling intervals tied to wall-clock seconds.

🚀 Common Use Cases

  • Digital clocks — show HH:MM:SS with padded seconds.
  • Log timestamps — include seconds in custom time strings.
  • Cron-like UI — trigger when getSeconds() === 0.
  • Animation frames — sync cues to second boundaries.
  • Form defaults — pre-fill second fields on time pickers.
  • Testing — assert second fields on constructed dates.

🧠 How getSeconds() Resolves the Value

1

Internal instant

The Date stores milliseconds since Unix epoch (UTC).

Storage
2

Local conversion

Engine applies the runtime timezone offset.

TZ
3

Extract seconds

Second 0–59 within the current minute is returned.

getSeconds
4

Use in logic

Format, compare, schedule, or pass to setSeconds.

📝 Notes

  • getSeconds() is local; use getUTCSeconds() when you standardize on UTC.
  • Range is 0–59 only for valid dates.
  • Not for elapsed duration—use getTime() or Date.now().
  • Invalid dates propagate NaN—validate first.
  • Pad with padStart(2, "0") when building fixed-width clocks.
  • For sub-second precision, add getMilliseconds() or use performance.now().

Browser & Runtime Support

Date.prototype.getSeconds() has been available since the first JavaScript Date implementation (ES1). It works in every browser and Node.js.

Baseline · ES1

Date.prototype.getSeconds()

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.getSeconds() Excellent

Bottom line: Safe everywhere. Remember: clock seconds (0–59) are not the same as elapsed milliseconds from getTime().

Conclusion

getSeconds() reads the local second component (0–59) from a JavaScript Date. Use it for clocks and wall-clock rules; use getTime() or Date.now() when you need elapsed duration.

Next, learn getTime() for full epoch milliseconds, or getMilliseconds() for sub-second fractions.

💡 Best Practices

✅ Do

  • Validate dates before calling getSeconds()
  • Pad seconds when building HH:MM:SS strings
  • Combine with hours and minutes for full clocks
  • Use getTime() for stopwatch duration
  • Pick UTC getters when storing UTC instants

❌ Don’t

  • Use getSeconds() for elapsed timer math
  • Confuse getSeconds() with getMilliseconds()
  • Ignore NaN from bad parses
  • Expect values outside 0–59 on valid dates
  • Log startTime.getSeconds() as “elapsed” in intervals

Key Takeaways

Knowledge Unlocked

Five things to remember about Date.getSeconds()

Your foundation for second reads in JavaScript.

5
Core concepts
🔢 02

0–59

Per minute.

Range
🌐 03

Local

Not UTC.

Timezone
🕐 04

HH:MM:SS

Clock format.

Pattern
05

Not elapsed

Use getTime.

Pitfall

❓ Frequently Asked Questions

An integer from 0 to 59 representing the seconds within the current minute of the Date object's local timezone.
getSeconds() uses local timezone rules. getUTCSeconds() reads the UTC second component. They can differ near minute boundaries across timezones.
getSeconds() returns 0–59 for whole seconds in the current minute. getMilliseconds() returns 0–999 for the fraction inside the current second.
NaN. Validate with !Number.isNaN(date.getTime()) before using the result in UI or conditionals.
No for valid dates—the range is 0–59. When seconds overflow via setSeconds, JavaScript normalizes into the next minute.
No. getSeconds() only reads the clock second (0–59), not elapsed duration. Use Date.now(), getTime(), or performance.now() for timers and benchmarks.
Did you know?

Calling startTime.getSeconds() inside setInterval shows the clock second (0–59), not how many seconds passed since startTime. For elapsed time, subtract Date.now() - start instead.

Continue to getTime()

Learn how to read the full milliseconds since the Unix epoch from a Date object.

getTime() 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