JavaScript getSeconds

The JavaScript getSeconds function is one of the date methods that extracts and returns the seconds value ranging from 0 to 59 from a given date according to local time.

Syntax

The syntax of the JavaScript getSeconds() function is

dateObject.getSeconds()

Return Value: The getSeconds() method returns an integer value between 0 and 59, representing the seconds from the given date according to the local time. When we use an invalid date, it returns NaN.

JavaScript getSeconds example

The following examples help understand the getSeconds() method.

Example 1: How to get the current seconds in JavaScript?

In the following example, the Date() constructor returns the current date and time. The getSeconds() function returns the number of seconds in the current date and time.

let dt = new Date();
let s = dt.getSeconds();

console.log(s);
console.log(dt);
44
2026-06-07T03:05:44.907Z

Example 2: Extract seconds from a custom date

In the example below, we use Date() to build a custom date and time. Next, we used the JavaScript getSeconds() to extract the seconds value from the custom date and time.

let dt = new Date('January 25, 2016 10:09:34');
console.log(dt.getSeconds());
34

Example 3: In this JavaScript get seconds example, we extract the seconds from a custom date without seconds (time). When you pass date without time, the Date() constructure assigns 00:00:00 to the date. So, the getSeconds() function will return 0 seconds.

let dt = new Date("January 25, 2016");
console.log(dt.getSeconds());
0

Example 4: If we use a getSeconds() function on a date with a seconds value greater than 59, it will return 0. In the example, we use the seconds value of 90, as we all know the seconds number should be between 0 and 59, the result becomes 0.

let dt = new Date("December 25, 2025 12:10:90");
console.log(dt.getSeconds());
0

Example 5: In the following program, we use the JavaScript getSeconds() function on an invalid date to see the NaN output. If we pass an invalid date, such as a date above 31 or a month greater than 12, return NaN. In the example below, December 32, 2025, is an invalid date.

let dt = new Date("December 32, 2025 12:10:90");
console.log(dt.getSeconds());
NaN

Example 6: We can use the getSeconds() method to set the countdown timer or print a value at fixed intervals. In the code below, we use getSeconds() to print the seconds value every two seconds. Please replace the seconds value with custom text to show the message.

function secondsClock()
{
  const dt = new Date();
  const s = dt.getSeconds();
  console.log(s);
}
setInterval(secondsClock, 2000);
59
1
3
5

JavaScript getSeconds 2 digits

We must convert the getSeconds() result to a string and then utilize the padStart() method with 2 and ‘0’ as the two arguments. It will return current seconds with a leading zero (seconds in 2 digits).

const dt = new Date();
let s = dt.getSeconds();
const sec = String(s).padStart(2, '0');
console.log(s);
console.log(sec);
2 
02

Difference between two dates in seconds

By default, when you subtract one date from another, it returns the date difference in milliseconds. So, we must divide the result by 1000 to get the difference between two dates in seconds.

const start = new Date("2026-06-13T09:00:00");
const end = new Date("2026-06-13T09:06:30");

const totalSeconds = (end - start) / 1000;
console.log(totalSeconds)
390