Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript

What is setMinutes in JavaScript?

Hassaan Waqar

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The setMinutes function is used on a Date object in JavaScript. It is used to set the minutes value of the date. The setMinutes take up to three parameters. The first parameter is the minutes value. It is compulsory to give this parameter. The second and third parameters are seconds and milliseconds, respectively. Both these parameters are optional.

The illustration below shows how the setMinutes function works in JavaScript:

How does setMinutes work in Javascript

Function prototype

The setMinutes function is defined as follows:

setMinutes(minutes, seconds, milliseconds)

The first parameter is compulsory. The other two are optional.

minutes can have an Int between 0-59.

seconds can have an Int between 0-59.

milliseconds can have an Int between 0-999.

Examples

The code below shows how the setMinutes function can be used in Javascript:

var myDate = new Date('June 29, 2021 23:15:30'); // setting a random date
myDate.setMinutes(17); // setting minutes
console.log("After setting minutes: " + myDate) // printing date
console.log("Now let's change all three values")
myDate.setMinutes(25, 14, 555); // setting minutes, seconds, and milliseconds
console.log("Updated date: " + myDate)

We can also use the getMinutes function to see if our changes have been made.

The getMinutes function returns the minutes value from a Date object.

The code below continues from the above code and outputs the minutes only:

var myDate = new Date('June 29, 2021 23:15:30'); // setting a random date
myDate.setMinutes(17); // setting minutes
console.log("After setting minutes: " + myDate) // printing date
console.log("Now let's change all three values")
myDate.setMinutes(25, 14, 555); // setting minutes, seconds, and milliseconds
console.log("Updated date: " + myDate)
console.log("Getting Minutes value: " + myDate.getMinutes())

As seen above, all updates are working successfully!

RELATED TAGS

javascript

CONTRIBUTOR

Hassaan Waqar
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring