JavaScript Date setMinutes() Method

The setMinutes() method in JavaScript is a method of the Date object that sets the minutes for a specified date according to local time.

The setMinutes() method takes three arguments: the minutes, the seconds (optional), and the milliseconds (optional). The minutes and seconds are represented as integers between 0 and 59, and the milliseconds are represented as an integer between 0 and 999.

Here is an example of how to use the setMinutes() method to set the minutes:

const date = new Date();
date.setMinutes(34);
console.log(date); // outputs the current date with the minutes set to 34

You can also pass a specific date as an argument to the Date object to set the minutes for that date:

Copy codeconst date = new Date("January 1, 2021 12:00:00");
date.setMinutes(34);
console.log(date); // outputs "Fri Jan 01 2021 12:34:00 GMT+0000 (Coordinated Universal Time)"

You can use the setMinutes() method to set the seconds and milliseconds as well:

const date = new Date();
date.setMinutes(34, 56, 789); // sets the minutes to 34, the seconds to 56, and the milliseconds to 789
console.log(date); // outputs "Fri Dec 17 2021 12:34:56 GMT+0000 (Coordinated Universal Time)"

Note that the setMinutes() method sets the minutes, seconds, and milliseconds according to local time, which is the time of the device running the code.

If you want to set the minutes, seconds, and milliseconds according to universal time, you can use the setUTCMinutes() method instead.

Here is an example that demonstrates the difference between setMinutes() and setUTCMinutes():

const date = new Date();
date.setMinutes(34);
const localDate = date.toString();

date.setUTCMinutes(34);
const utcDate = date.toString();

console.log(localDate); // outputs the current date with the minutes set to 34 according to local time
console.log(utcDate); // outputs the current date with the minutes set to 34 according to universal time

Leave a comment

Blog at WordPress.com.