How to use setMinutes in JavaScript

In this article, I will explain use of setMinutes() method in JavaScript date object.
  • 1494
JavaScript setMinutes() method
  • setMinutes() method of date method sets the minutes for a given date according to local time.
  • setMinutes() method can also be used to set the seconds and milliseconds.

Syntax

Date.setMinutes(min,sec,millisec)

  • Min : Min is optional parameter. An integer is representing the minutes and it's value between 0 and 59.

  • Sec : Sec is optional parameter. An integer is representing the seconds and it's value between 0 and 59,. If you given the secondsValue parameter, you must also give the minutesValue.

  • Mili : Mili is optional parameter. A number is representing the milliseconds and it's value between 0 and 999. If you give the Mili parameter, you must also give the Min and Sec.

Example

In the following example show to setMinutes() method set the date time to be 60 minutes ago.

<!DOCTYPE html>

<html>

<body>

<h3 style="color:Teal ">JavaScript setMinutes() Method Example</h3>

<p id="demo">Click for display the date after setting the time to be 60 minutes ago.</p>

<button onclick="myFunction()">Click On</button>

<script type="text/javascript">

    function myFunction() {

        var d = new Date();

        d.setMinutes(d.getMinutes() - 60);

        var x = document.getElementById("demo");

        x.innerHTML = "60 minute ago time is : "+d;

    }

</script>

</body>

</html>

Output1

setMinute 1.jpg

Output2

setMinute 2.jpg

You may also want to read these related articles here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.