JavaScript Date setMinutes()

Posted by

JavaScript Date setMinutes() Tutorial

The Date.setMinutes() method in JavaScript is used to set the minutes of a given Date object relative to the local time. This method can be used to add minutes to the Date object, or to reset the minutes of the Date object to a specific number.

Syntax

The syntax for the Date.setMinutes() method is as follows:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

dateObj.setMinutes(minutes [, seconds, millisecond])

[/dm_code_snippet]

Where:

  • dateObj is an instance of the Date object.
  • minutes is the number of minutes to set the Date object to.
  • seconds is an optional parameter and is the number of seconds to set the Date object to.
  • millisecond is an optional parameter and is the number of milliseconds to set the Date object to.

Return Value

The Date.setMinutes() method returns the number of milliseconds since January 1, 1970 00:00:00 UTC.

Examples

Let’s look at some examples of how to use the Date.setMinutes() method.

The first example creates a Date object and sets it to the current date and time:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

var d = new Date();

[/dm_code_snippet]

The next example uses the setMinutes() method to set the minutes of the Date object to 30 minutes:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

d.setMinutes(30);

[/dm_code_snippet]

The next example adds 10 minutes to the Date object:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

d.setMinutes(d.getMinutes() + 10);

[/dm_code_snippet]

And finally, the last example sets the Date object to the current date and time and then sets the minutes to 30:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

d.setMinutes(30, 0, 0);

[/dm_code_snippet]

Conclusion

In this tutorial, we discussed the Date.setMinutes() method in JavaScript. We looked at the syntax of the method, the return value, and some examples of how to use the method to set the minutes of a Date object.