The getMinutes()
method in JavaScript is a part of the Date
object and is used to get the minutes (0-59) for a given date object. This can be useful for getting the current time or for working with specific dates and times.
Here’s an example of how to use getMinutes()
to get the current minutes:
let date = new Date();
let minutes = date.getMinutes();
console.log(minutes); // Outputs the current minutes
You can also pass a date object as an argument to getMinutes()
to get the minutes for a specific date:
let date = new Date(2022, 0, 1, 11, 30, 0); // January 1, 2022 at 11:30:00
let minutes = date.getMinutes();
console.log(minutes); // Outputs 30
Note that the getMinutes()
method returns the minutes in the local time zone. If you want to get the minutes in a specific time zone, you can use the getUTCMinutes()
method instead.
Here’s an example of how to use getUTCMinutes()
to get the minutes for a date in UTC time:
let date = new Date(2022, 0, 1, 11, 30, 0); // January 1, 2022 at 11:30:00 (local time)
let minutes = date.getUTCMinutes(); // Get the minutes in UTC time
console.log(minutes); // Outputs 30
In addition to getting the minutes, you can also use the setMinutes()
method to set the minutes for a date object. Here’s an example of how to use it:
let date = new Date();
date.setMinutes(45); // Set the minutes to 45
console.log(date); // Outputs the current date and time with the minutes set to 45
Leave a Reply