JavaScript date.setMilliseconds(milliseconds) method
The date.setMilliseconds(milliseconds)
method in JavaScript is used to set the milliseconds for a specified Date
object according to local time. This method modifies the original Date
object and can be used to adjust the time down to the millisecond level.
Syntax:
Parameters:
- milliseconds: An integer representing the milliseconds (from 0 to 999).
Returns:
- The number of milliseconds since January 1, 1970, 00:00:00 UTC, representing the updated
Date
object.
Example 1: Setting Milliseconds
Output:
Explanation:
- The original date of October 22, 2024, 12:30:45.123 is updated to 12:30:45.456.
Example 2: Setting Milliseconds with Current Time
Output:
Explanation:
- The
setMilliseconds(999)
method updates the milliseconds of the current date to 999.
Example 3: Handling Time Adjustments
Output:
Explanation:
- When setting the milliseconds to 1000, the
Date
object automatically rolls over to the next second, resulting in 12:31:00.000.
Summary:
date.setMilliseconds(milliseconds)
allows you to set the milliseconds for aDate
object, affecting the time representation.- It modifies the original
Date
object and can trigger automatic adjustments if the specified milliseconds cause the time to exceed normal bounds (e.g., rolling over to the next second).