JavaScript Date setUTCSeconds()
Syntax & Examples


Date.setUTCSeconds() method

The setUTCSeconds() method sets the seconds for a specified date according to universal time.


Syntax of Date.setUTCSeconds()

There are 2 variations for the syntax of Date.setUTCSeconds() method. They are:

1.
setUTCSeconds(secondsValue)

Parameters

ParameterOptional/RequiredDescription
secondsValuerequiredAn integer between 0 and 59, representing the seconds.

This method sets the seconds for the specified date according to universal time.

Returns value of type Number.

2.
setUTCSeconds(secondsValue, msValue)

Parameters

ParameterOptional/RequiredDescription
secondsValuerequiredAn integer between 0 and 59, representing the seconds.
msValueoptionalAn integer between 0 and 999, representing the milliseconds.

This method sets the seconds and milliseconds for the specified date according to universal time.

Returns value of type Number.



✐ Examples

1 Using setUTCSeconds() method with one argument

In JavaScript, we can use the setUTCSeconds() method to set the seconds of a date according to universal time.

For example,

  1. Create a new Date object with the current date and time.
  2. Use the setUTCSeconds() method to set the seconds to 30 UTC.
  3. Log the updated date to the console using console.log().

JavaScript Program

const date = new Date();
date.setUTCSeconds(30);
console.log(date);

Output

Expected output: The date with the seconds set to 30 UTC.

2 Using setUTCSeconds() method with two arguments

In JavaScript, we can use the setUTCSeconds() method to set the seconds and milliseconds of a date according to universal time.

For example,

  1. Create a new Date object with the current date and time.
  2. Use the setUTCSeconds() method to set the seconds to 30 UTC and the milliseconds to 500.
  3. Log the updated date to the console using console.log().

JavaScript Program

const date = new Date();
date.setUTCSeconds(30, 500);
console.log(date);

Output

Expected output: The date with the seconds set to 30 UTC and the milliseconds set to 500.

Summary

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