JavaScript Date setMinutes() Method
Last Updated :
19 May, 2023
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
let dateobj = new Date( 'October 13, 1996' );
dateobj.setMinutes(51);
let B = dateobj.getMinutes();
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
let dateobj = new Date();
dateobj.setMinutes(42);
let B = dateobj.getMinutes();
let C = dateobj.getMonth();
let D = dateobj.getDate();
let E = dateobj.getFullYear();
console.log(B);
console.log(C);
console.log(D);
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
let dateobj =
new Date( 'October 13, 1996 05:35:32' );
dateobj.setMinutes(66);
let B = dateobj.getMinutes();
let C = dateobj.getHours();
console.log(B);
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.
Share your thoughts in the comments
Please Login to comment...