JavaScript date.setMinutes(minutes, seconds, milliseconds) method


The date.setMinutes(minutes, seconds, milliseconds) method in JavaScript is used to set the minutes for a Date object according to local time. Optionally, you can also set the seconds and milliseconds. This method updates the original Date object and allows you to adjust multiple components of time at once.

Syntax:

date.setMinutes(minutes[, seconds[, milliseconds]]);

Parameters:

  • minutes: An integer between 0 and 59 representing the minutes.
  • seconds (optional): An integer between 0 and 59 representing the seconds. Defaults to the current seconds if not provided.
  • milliseconds (optional): An integer between 0 and 999 representing the milliseconds. Defaults to the current milliseconds if not provided.

Returns:

  • The number of milliseconds since January 1, 1970, 00:00:00 UTC, representing the updated Date object.

Example 1: Setting Only the Minutes

const date = new Date('2024-10-22T12:15:30.000Z'); // October 22, 2024, 12:15:30 date.setMinutes(45); // Change minutes to 45 console.log(date);

Output:

2024-10-22T12:45:30.000Z

Explanation:

  • The original minutes (15) are changed to 45, while the seconds and milliseconds remain the same.

Example 2: Setting Minutes and Seconds

const date = new Date('2024-10-22T12:15:30.000Z'); // October 22, 2024, 12:15:30 date.setMinutes(50, 10); // Change minutes to 50, seconds to 10 console.log(date);

Output:

2024-10-22T12:50:10.000Z

Explanation:

  • Both the minutes are set to 50 and the seconds to 10, updating the Date object accordingly.

Example 3: Setting Minutes, Seconds, and Milliseconds

const date = new Date('2024-10-22T12:15:30.123Z'); // October 22, 2024, 12:15:30.123 date.setMinutes(55, 45, 500); // Change minutes to 55, seconds to 45, and milliseconds to 500 console.log(date);

Output:

2024-10-22T12:55:45.500Z

Explanation:

  • The setMinutes(55, 45, 500) method updates the time to 12:55:45.500, modifying the minutes, seconds, and milliseconds.

Example 4: Handling Overflow

If the minutes, seconds, or milliseconds exceed their normal range, the method will roll over to the next hour, second, or minute automatically.

const date = new Date('2024-10-22T12:55:59.900Z'); // October 22, 2024, 12:55:59.900 date.setMinutes(56, 60); // Set minutes to 56, seconds to 60 (overflow) console.log(date);

Output:

2024-10-22T12:57:00.000Z

Explanation:

  • Since the seconds are set to 60, the method rolls over the time to the next minute, resulting in 12:57:00.000.

Example 5: Using Default Values

If you only provide the minutes argument, the seconds and milliseconds will remain unchanged.

const date = new Date('2024-10-22T12:15:30.456Z'); // October 22, 2024, 12:15:30.456 date.setMinutes(20); // Change minutes to 20 console.log(date);

Output:

2024-10-22T12:20:30.456Z

Explanation:

  • Only the minutes are changed, while the seconds (30) and milliseconds (456) stay the same.

Summary:

  • date.setMinutes(minutes, seconds, milliseconds) allows you to set the minutes, and optionally the seconds and milliseconds, for a Date object.
  • It modifies the original Date object and automatically adjusts overflow if any component exceeds its normal range.