JavaScript Date getUTCSeconds()
Syntax & Examples


Date.getUTCSeconds() method

The getUTCSeconds() method of the Date object in JavaScript returns the seconds (0 – 59) in the specified date according to universal time.


Syntax of Date.getUTCSeconds()

The syntax of Date.getUTCSeconds() method is:

getUTCSeconds()

This getUTCSeconds() method of Date returns the seconds (0 – 59) in the specified date according to universal time.

Return Type

Date.getUTCSeconds() returns value of type Number.



✐ Examples

1 Using getUTCSeconds() to get the UTC seconds of a specific date

In JavaScript, we can use the getUTCSeconds() method to get the seconds from a Date object according to universal time.

For example,

  1. We create a new Date object date representing January 15, 2021, at 15:30:45 UTC.
  2. We use the getUTCSeconds() method to get the seconds from date.
  3. The seconds are stored in the variable utcSeconds.
  4. We log utcSeconds to the console using console.log() method.

JavaScript Program

const date = new Date('2021-01-15T15:30:45Z');
const utcSeconds = date.getUTCSeconds();
console.log(utcSeconds);

Output

45

2 Using getUTCSeconds() with the current date

In JavaScript, we can use the getUTCSeconds() method to get the current seconds from the current date according to universal time.

For example,

  1. We create a new Date object now representing the current date and time.
  2. We use the getUTCSeconds() method to get the current seconds from now.
  3. The current seconds are stored in the variable currentUTCSeconds.
  4. We log currentUTCSeconds to the console using console.log() method.

JavaScript Program

const now = new Date();
const currentUTCSeconds = now.getUTCSeconds();
console.log(currentUTCSeconds);

Output

30

3 Using getUTCSeconds() with a specific date in the past

In JavaScript, we can use the getUTCSeconds() method to get the seconds from a specific date in the past according to universal time.

For example,

  1. We create a new Date object pastDate representing July 4, 1776, at 12:00:30 UTC.
  2. We use the getUTCSeconds() method to get the seconds from pastDate.
  3. The seconds are stored in the variable pastUTCSeconds.
  4. We log pastUTCSeconds to the console using console.log() method.

JavaScript Program

const pastDate = new Date('1776-07-04T12:00:30Z');
const pastUTCSeconds = pastDate.getUTCSeconds();
console.log(pastUTCSeconds);

Output

30

Summary

In this JavaScript tutorial, we learned about getUTCSeconds() method of Date: the syntax and few working examples with output and detailed explanation for each example.