JavaScript getSeconds()

getSeconds() returns the seconds component of a JavaScript Date according to local time. The normal range is 0 to 59.

const now = new Date();
const value = now.getSeconds();
console.log(value);

getSeconds() Syntax and Return Value Top ↑

const value = date.getSeconds();

The method takes no parameters. If the Date is invalid, it returns NaN.

Pad Seconds to Two Digits Top ↑

const value = String(date.getSeconds()).padStart(2, "0");

Using getSeconds() in a Clock Top ↑

const now = new Date();
const hh = String(now.getHours()).padStart(2, "0");
const mm = String(now.getMinutes()).padStart(2, "0");
const ss = String(now.getSeconds()).padStart(2, "0");
console.log(`${{hh}}:${{mm}}:${{ss}}`);

For an updating display, see the JavaScript clock and timer tutorial.

getSeconds() and Its UTC Equivalent Top ↑

const localValue = date.getSeconds();
const utcValue = date.getUTCSeconds();

Use the local method for the host's local time and the UTC method when your logic is explicitly based on UTC. See UTC date methods.

Invalid Dates Top ↑

const bad = new Date("invalid");
console.log(bad.getSeconds()); // NaN

Previous: getMinutes()

Seconds vs Milliseconds Top ↑

getSeconds() returns whole seconds. Use getMilliseconds() for the 0–999 millisecond component or getTime() for the complete timestamp.

const seconds = date.getSeconds();
const milliseconds = date.getMilliseconds();

The original demo endpoint remains available at seconds demo.

Common getSeconds() Mistakes Top ↑

  • Assuming the returned number is already zero-padded for display.
  • Mixing local and UTC components in the same calculation.
  • Ignoring invalid Date values.
  • Using raw numeric components when locale-aware formatting would better serve users.

Date Reference




Subscribe to our YouTube Channel here



plus2net.com










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer