• JavaScript Video Tutorials

JavaScript Date setUTCSeconds() Method



The JavaScript Date.setUTCSeconds() method is used to set the "seconds" of a Date object according to Coordinated Universal Time (UTC). The return value of this method will be an updated timestamp of the Date object, which reflects the changes made by modifying the seconds component. Additionally, we can modify the "milliseconds" of the date object.

UTC, also known as Universal Time Coordinated, is the time established by the World Time Standard. UTC is equivalent to GMT (Greenwich Mean Time), ensuring the uniformity in time measurement across the world.

Syntax

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

setUTCSeconds(secondsValue, millisecondsValue);

Parameters

This method accepts two parameters. The same is described below −

  • secondsValue An integer representing the seconds (0 to 59).
    • If -1 is provided, it will result in the last second of the previous minute.
    • If 60 is provided, it will result in the first second of the next minute.
  • millisecondsValue (optional) An integer representing the milliseconds (0 to 999).
    • If -1 is provided, it will result in the last millisecond of the previous second.
    • If 1000 is provided, it will result in the first millisecond of the next second.

Return Value

This method returns the number of milliseconds between the generated date and midnight January 1, 1970, Universal Coordinated Time (UTC).

Example 1

In the following example, we are using the JavaScript Date setUTCSeconds() method to set the "seconds" to 30, according to UTC time −

<html>
<body>
<script>
   const currentDate = new Date();
   currentDate.setUTCSeconds(30);

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

Output

If we execute the above program, the seconds will be set to 30.

Example 2

If we provide "-1" for secondsValue, this method will gives the last second of the previous minute −

<html>
<body>
<script>
   const currentDate = new Date("2023-12-25 18:30:10");
   currentDate.setUTCSeconds(-1);

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

Output

It will return "59" as the last second of previous minute (29).

Example 3

If we provide "60" for secondsValue, this method will gives the first second of the next minute −

<html>
<body>
<script>
   const currentDate = new Date("2023-12-25 18:30:10");
   currentDate.setSeconds(60);

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

Output

It will return "0" as the last second of previous minute (31).

Example 4

In the following example, we are setting the "milliseconds" of a date object along with the "seconds" −

<html>
<body>
<script>
   const currentDate = new Date("2023-12-25 18:30:10");
   currentDate.setSeconds(35, 697);

   document.write("UTCSeconds: ", currentDate.getUTCSeconds(), "
","UTCMilliseconds: ", currentDate.getMilliseconds()); </script> </body> </html>

Output

If we execute the above program, the seconds will be set to "35" and milliseconds to "697".

Example 5

If we pass a NaN value as a parameter to this function, the date will be set to "Invalid date" and "NaN" is returned as result −

<html>
<body>
<script>
   const currentDate = new Date();
   currentDate.setUTCSeconds("Hle");

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

Output

As we can see the output, "NaN" is returned as output.

Advertisements