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:
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
Output:
Explanation:
- The original minutes (15) are changed to 45, while the seconds and milliseconds remain the same.
Example 2: Setting Minutes and Seconds
Output:
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
Output:
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.
Output:
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.
Output:
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 aDate
object.- It modifies the original
Date
object and automatically adjusts overflow if any component exceeds its normal range.