JavaScript date.setMinutes() method is used to set minutes into a Date object which is created using the Date() constructor.
Syntax:
DateObj.setMinutes(Minutes_Value)
Parameter: This method accepts a single parameter as mentioned above and described below:
- minutes_Value: This parameter holds the value of minutes which is used to set in the Date() constructor.
Return Value: It returns the new date with the updated minute which is set by the setMinutes() method.
Note: The DateObj is a valid Date object created using the Date() constructor in which we want to set the minute. The value of the minute is from 0 to 59.
The below examples illustrate the JavaScript Date setMinutes() Method:
Example 1: If in the Date() constructor we do not give any minute while creating the Date object, still setMinutes() method set a new minute which is given as its parameter.
javascript
// Here minute has not been assigned // while creating Date object let dateobj = new Date( 'October 13, 1996' ); // new minute of 51 is being set in above Date // Object with the help of setMinutes() method dateobj.setMinutes(51); // new minute from above Date Object is // being extracted using getMinutes() let B = dateobj.getMinutes(); // Printing new minute console.log(B); |
Output:
51
Example 2: If nothing as a parameter is given in the Date() constructor, still setMinutes() method set minutes but a month, year, date, etc become current ones. Here 42 is the new minutes, 3 is the current month i.e. April, 1 is the current date and 2018 is the current year.
javascript
// Here nothing has been assigned // while creating Date object let dateobj = new Date(); // New minute of 42 is being set in above Date // Object with the help of setMinutes() method dateobj.setMinutes(42); // Minutes from above Date Object is // being extracted using getMinutes() let B = dateobj.getMinutes(); // Month from above Date Object is // being extracted using getMonth() let C = dateobj.getMonth(); // Date from above Date Object is // being extracted using getDate() let D = dateobj.getDate(); // Year from above Date Object is // being extracted using getFullYear() let E = dateobj.getFullYear(); // Printing new minutes console.log(B); // Printing current month console.log(C); // Printing current date console.log(D); // Printing current year console.log(E); |
Output:
42 3 1 2018
Example 3: If the value of the minute is 66 as given in the parameter of the setMinutes() function, It will set 6 as the minute because the minute range is from 0 to 59 and (66%60 = 6). Here 6 is the new minute and the hour becomes 06 from 05 because the minute range is from 0 to 59 i.e., a total of 60 and we set the new minute as 66 which increases the hour by one to 06 from 05 and the minute becomes 6.
javascript
// Here date has been assigned // while creating Date object let dateobj = new Date( 'October 13, 1996 05:35:32' ); // new minute of 66 is being set in above Date // Object with the help of setMinutes() function dateobj.setMinutes(66); // minute from above Date Object is // being extracted using getMinutes() let B = dateobj.getMinutes(); // Hour from above Date Object is // being extracted using getHours() let C = dateobj.getHours(); // Printing new minute console.log(B); // Printing hour console.log(C); |
Output:
6 6
Supported Browsers:
- Google Chrome 1 and above
- Edge 12 and above
- Firefox 1 and above
- Internet Explorer 3 and above
- Opera 3 and above
- Safari 1 and above
We have a complete list of Javascript Javascript Date methods, to check those please go through the Javascript Date Object Complete Reference article.