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,
- We create a new Date object
date
representing January 15, 2021, at 15:30:45 UTC. - We use the
getUTCSeconds()
method to get the seconds fromdate
. - The seconds are stored in the variable
utcSeconds
. - We log
utcSeconds
to the console usingconsole.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,
- We create a new Date object
now
representing the current date and time. - We use the
getUTCSeconds()
method to get the current seconds fromnow
. - The current seconds are stored in the variable
currentUTCSeconds
. - We log
currentUTCSeconds
to the console usingconsole.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,
- We create a new Date object
pastDate
representing July 4, 1776, at 12:00:30 UTC. - We use the
getUTCSeconds()
method to get the seconds frompastDate
. - The seconds are stored in the variable
pastUTCSeconds
. - We log
pastUTCSeconds
to the console usingconsole.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.