JavaScript Date setUTCMinutes() Method



In JavaScript, the Date.setUTCMinutes() method is used to set the minutes of a Date object according to the UTC (Coordinated Universal Time) time zone. This method allows you to adjust the minutes component of the date without affecting other parts of the date (such as the hour, day, month, or year).

If the provided parameter for this method is "NaN", the date will be set to "Invalid Date" and "NaN" is returned.

Syntax

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

setUTCMinutes(minutesValue, secondsValue, msValue)

Parameters

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

  • minutesValue − An integer between 0 and 59, representing the minutes.
  • secondsValue (optional) − An integer representing the seconds value (0-59).
  • msValue (optional) − An integer representing the milliseconds value (0-999).

Return value

This method returns the number of milliseconds between January 1, 1970 00:00:00 UTC and the updated date object after setting the specified minutes, seconds, and milliseconds.

Example 1

In the following example, we are using the JavaScript Date.setUTCMinutes() method to set the "minute" value to "15", according to UTC time −

<html>
<body>
<script>
   let date = new Date();
   let result = date.setUTCMinutes(15);
   document.write(result);
</script>
</body>
</html>

Output

After executing the above program, it returns the number of milliseconds between EPOCH and the updated date object.

Example 2

In the exmaple, we are setting the minutes to a specific value "15" −

<html>
<body>
<script>
   let date = new Date();
   date.setUTCMinutes(15);
   document.write(date);
</script>
</body>
</html>

Output

If we execute the above program, it sets the minutes (according to UTC time) of the date object.

Example 3

Here, we are incrementing the minutes of the current UTC time by "15" &minus

<html>
<body>
<script>
   let date = new Date();
   date.setUTCMinutes(date.getUTCMinutes() + 15);
   document.write(date);
</script>
</body>
</html>

Output

After executing the above program, it returns the updated date with minutes incremented by 15.

Example 4

If the provided parameter for setUTCMinutes() method is "NaN", the date will be set to "invalid date" and "NaN" will returned as result −

<html>
<body>
<script>
   let date = new Date();
   date.setUTCMinutes(NaN);
   document.write(date, "<br>");
   document.write(date.getUTCMinutes());
</script>
</body>
</html>

Output

If we execute the above program, it returns NaN as result.

Advertisements