• JavaScript Video Tutorials

JavaScript Date setMinutes() Method



In JavaScript, the Date.setMinutes() method is used to set the minutes value of a Date object to a specified numeric value (between 0 and 59). It allows us to modify the minute component of a Date object without changing other parts of the date and time. The setMinutes() method does not create a new Date object but instead modifies the current date object.

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

Syntax

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

setMinutes(minutesValue, secondsValue, msValue)

Parameters

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

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

Return value

It returns the number of milliseconds between January 1, 1970, 00:00:00 UTC and the updated Date object.

Example 1

In the example below, we are using the JavaScript Date.setMinutes() method to set the minutes of the current date object to "30" −

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

Output

If we execute the above program, we can see that the value of minutes segment has been set to 30.

Example 2

In this example below, we are adding 15 minutes to the current time using the getMinutes() method −

<html>
<body>
<script>
   let date = new Date();
   document.write(date, "<br>");
   date.setMinutes(date.getMinutes() + 15);
   document.write(date.getMinutes());
</script>
</body>
</html>

Output

As we can see in the output, 15 minutes have been added to the current date.

Example 3

If the provided parameter for setMinutes() 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.setMinutes(NaN);
   document.write(date, "<br>")
   document.write(date.getMinutes());
</script>
</body>
</html>

Output

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

Advertisements