The JavaScript setMilliseconds function is a Date Function, which is used to set the milliseconds of a given date according to the local time zone.
The syntax of the setMilliseconds function is:
Date.setMilliseconds(Milliseconds)
Parameter: The JavaScript setMilliseconds() function accepts a milliseconds value between 0 and 999 to pass it to the original date. If you pass 999 or above (for Example, 10001), it adds 1 second to the existing date and sets 5 as the milliseconds.
Return Value: This method returns a new and updated date with the given milliseconds. If the parameter value is NaN or undefined, the date becomes invalid, and the result is NaN.
JavaScript setMilliseconds example
In the following example, we use the setMilliseconds on the current date and time to set milliseconds to 222. To show the difference, we call the current date and time before and after applying the setMilliseconds() method. Apart from that, we call the getMilliseconds() method to extract the milliseconds part.
let dt = new Date();
console.log(dt.toISOString());
dt.setMilliseconds(222);
console.log(dt.toISOString());
console.log(dt.getMilliseconds());
2026-06-14T09:37:44.102Z
2026-06-14T09:37:44.222Z
222
Example 2: In this example, we set the milliseconds to the custom date. Here, the Date() constructor parses the string date and converts it to a date and time.
Here, we haven’t provided any milliseconds in the custom date, so they are set to 000 by default. Next, the JavaScript setMilliseconds() method sets the milliseconds value to 111.
let dt = new Date("January 1, 2017 23:45:22");
console.log(dt.toISOString());
dt.setMilliseconds(111);
console.log(dt.toISOString());
2017-01-01T18:15:22.000Z
2017-01-01T18:15:22.111Z
Example 3: Similar to the above, if we use the setMilliseconds() method on a custom date without providing any time value. The Date() object sets the time as 00:00:00.000. Next, the setMilliseconds() will add the given milliseconds to this date.
let dt = new Date("January 1, 2017");
console.log(dt.toISOString());
dt.setMilliseconds(111);
console.log(dt.toISOString());
2016-12-31T18:30:00.000Z
2016-12-31T18:30:00.111Z
Example 4: As we mentioned earlier, the milliseconds value must range from 0 to 999. If we pass a number greater than that, it converts milliseconds to seconds and adds those seconds to the current date and time.
In the example below, we are using JavaScript setMilliseconds to set the milliseconds to 4500000 of the current date. If you convert 4500000 milliseconds, it’s about 1 hour and 15 minutes. The setMilliseconds() method adds 1 hour 15 minutes to the current date and time.
let dt = new Date();
console.log(dt);
dt.setMilliseconds(4500000);
console.log(dt);
Sat Jun 13 2026 15:43:19 GMT+0530 (India Standard Time)
Sat Jun 13 2026 16:58:19 GMT+0530 (India Standard Time)
Example 5: In this JavaScript set Milliseconds example, we set the milliseconds of a custom date to 5900000.
let dt = new Date("January 1, 2017 23:45:22");
dt.setMilliseconds(5900000);
console.log(dt);
Mon Jan 02 2017 01:23:42 GMT+0530 (India Standard Time)
Example 6: Using setMilliseconds() with negative values
If we set the milliseconds to -1, it returns the last millisecond from the previous second. If you observe the result, 42 seconds become 41, and the milliseconds value is 999 because 41.999 is the last millisecond from the previous second.
let dt = new Date();
console.log(dt.toISOString());
dt.setMilliseconds(-1);
console.log(dt.toISOString());
2026-06-14T09:57:42.423Z
2026-06-14T09:57:41.999Z
How to add 3 days in milliseconds to the current date?
We can use the JavaScript setMilliseconds() method or the getTime() method to add 3 days in milliseconds to the current date or a custom datetime for future predictions.
3 days = 3 × 24 × 60 × 60 × 1000 = 259,200,000 milliseconds
Using setMilliseconds
let ms = 259200000;
let dt = new Date();
console.log(dt.toISOString());
dt.setMilliseconds(ms);
console.log(dt.toISOString());
2026-06-14T09:55:26.931Z
2026-06-17T09:55:26.931Z
Using setTime() and getTime()
let ms = 259200000;
let dt = new Date();
console.log(dt.toISOString());
dt.setTime(dt.getTime() + ms);
console.log(dt.toISOString());
2026-06-14T09:55:26.931Z
2026-06-17T09:55:26.931Z