JavaScript Date getUTCSeconds() Method



The getUTCSeconds() method is a part of the JavaScript Date object's prototype. It is used to retrieve the seconds component of a date object according to Coordinated Universal Time (UTC). The return value will be an integer between (0 to 59) specifies seconds of a date. If the provided Date object is "invalid", this method returns Not a Number (NaN) as result. Additionally, this method doesn't accept any parameters.

UTC stands for Coordinated Universal Time. It is the primary time standard by which the world regulates clocks and time. Whereas, the India Standard Time (IST) is the time observed in India, and the time difference between IST and UTC is as UTC+5:30 (i.e. 5 hours 30 minutes).

Syntax

Following is the syntax of JavaScript Date getUTCSeconds() method −

getUTCSeconds();

This method does not accept any parameter.

Return Value

The return value is an integer representing the seconds component of the given date object in UTC time zone.

Example 1

In the following example, we are demonstrating the basic usage of JavaScript Date getUTCSeconds() method −

<html>
<body>
<script>
   const currentDate = new Date();
   const seconds = currentDate.getUTCSeconds();

   document.write(seconds);
</script>
</body>
</html>

Output

It returns the seconds component of a date according to universal time.

Example 2

In this example, we retrieving and printing the seconds component from the provided date −

<html>
<body>
<script>
   const specificDate = new Date("December 21, 2023 12:30:45 UTC");
   const seconds = specificDate.getUTCSeconds();

   document.write(seconds);
</script>
</body>
</html>

Output

The above program returns integer 45 as seconds value.

Example 3

The below example prints the seconds component from a date according to Universal time for every 2 seconds.

<html>
<body>
<script>
   function printSeconds() {
      const currentDate = new Date();
      const seconds = currentDate.getUTCSeconds();
      document.write(seconds + "<br>");
   }

   setInterval(printSeconds, 2000);
</script>
</body>
</html>

Output

As we can see the output, the seconds are printing for every 2 seconds.

Advertisements