setMilliseconds() writes the sub-second fraction (0–999) within the current local second on a Date object. This guide covers syntax, zeroing fractional noise, overflow, and comparisons with epoch timestamps.
01
Syntax
setMilliseconds(n)
02
0–999
Sub-second
03
Not getTime
Clock fraction
04
Overflow
Rolls seconds
05
Mutates
In place
06
Returns
Epoch ms
Fundamentals
Introduction
Timestamps from APIs often include fractional seconds. After reading them with getMilliseconds(), setMilliseconds() lets you write that sub-second part in local time.
This is not for measuring elapsed time—for duration, subtract getTime() values. setMilliseconds() adjusts the millisecond hand of the local clock only.
Concept
Understanding the setMilliseconds() Method
Date.prototype.setMilliseconds(milliseconds) accepts an integer. Values 0–999 set the fraction inside the current second. Values outside that range normalize—setMilliseconds(1500) adds one second and leaves 500 ms.
The method mutates the Date and returns epoch milliseconds. For UTC sub-second writes, use setUTCMilliseconds(). To set hour through ms at once, use setHours(h, m, s, ms).
💡
Beginner Tip
To strip sub-second noise: date.setMilliseconds(0). For exact timestamps: date.setHours(12, 30, 45, 250).
Foundation
📝 Syntax
JavaScript
dateObj.setMilliseconds(milliseconds)
Parameters
milliseconds — Integer for the ms within the current local second (overflow normalizes).
Return value
Updated epoch milliseconds after the change.
NaN if the Date was invalid before the call.
Cheat Sheet
⚡ Quick Reference
Goal
Code
Set ms to 500
date.setMilliseconds(500)
Zero sub-seconds
date.setMilliseconds(0)
Full time with ms
date.setHours(12, 30, 45, 250)
Read ms after set
date.getMilliseconds()
Elapsed duration
end.getTime() - start.getTime()
Clone before mutate
const copy = new Date(date)
Compare
📋 setMilliseconds() vs Similar Methods
These APIs involve milliseconds but serve different jobs.
setMilliseconds()
0–999
Local sub-second
getMilliseconds()
read
Getter pair
getTime()
epoch ms
Full instant
Date.now()
now ms
Current timestamp
Hands-On
Examples Gallery
Open DevTools Console (F12) or use Try-it links. Fixed times use the Date constructor for predictable local values.
📚 Getting Started
Change the millisecond fraction inside the current second.
Example 1 — Set Milliseconds to 500
Update only the sub-second part; seconds and above stay the same.
JavaScript
const date = new Date(2026, 2, 1, 12, 30, 45, 120);
date.setMilliseconds(500);
console.log(date.getSeconds()); // 45
console.log(date.getMilliseconds()); // 500
1500 ms = 1 second + 500 ms. The engine normalizes instead of clamping to 999.
Applications
🚀 Common Use Cases
API timestamps — apply fractional seconds from ISO strings.
Second rounding — setMilliseconds(0) before second-level compare.
Test fixtures — pin exact ms in unit tests.
Log alignment — strip ms for grouped log buckets.
Animation keys — snap frame times (prefer performance.now() for benchmarks).
Clone + adjust — keep originals when tweaking sub-seconds.
🧠 How setMilliseconds() Updates the Clock
1
Read local time
Engine loads the current local second and its fraction.
Local
2
Apply new ms
The milliseconds argument replaces the sub-second fraction.
setMilliseconds
3
Normalize overflow
Values ≥ 1000 or < 0 roll into adjacent seconds.
Normalize
4
⏱
Mutate & return ms
Date updates in place; epoch milliseconds are returned.
Important
📝 Notes
setMilliseconds() is 0–999 inside the current second—not epoch ms.
For elapsed duration, use end.getTime() - start.getTime().
Mutates the original Date—clone when preserving the old value.
Returns epoch ms, not the Date—no chaining on the return value.
Overflow normalizes into seconds (1500 → +1 s, 500 ms).
Use setUTCMilliseconds for UTC sub-second rules.
Compatibility
Browser & Runtime Support
Date.prototype.setMilliseconds() has been available since ES1. It works in every browser and Node.js.
✓ Baseline · ES1
Date.prototype.setMilliseconds()
Supported in Chrome, Firefox, Safari, Edge, Internet Explorer, and all Node.js versions. No polyfill required.
99%Universal API
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
Date.setMilliseconds()Excellent
Bottom line: Safe everywhere. Remember sub-second range, mutation, and getTime() for duration.
Wrap Up
Conclusion
setMilliseconds() writes the local sub-second fraction (0–999) on a JavaScript Date. Use it to adjust fractional seconds; use getTime() for measuring elapsed time.
Assume values clamp at 999 (overflow rolls seconds)
Use busy loops with Date for micro-benchmarks
Mix local setters with UTC getters in one label
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Date.setMilliseconds()
Your foundation for local sub-second writes in JavaScript.
5
Core concepts
📝01
Syntax
setMilliseconds(n)
API
🔢02
0–999
Sub-second.
Range
⏱03
Not duration
Use getTime().
Pitfall
📈04
Overflow
Rolls seconds.
Normalize
🔄05
Clone
Keep original.
Pattern
❓ Frequently Asked Questions
It sets the milliseconds within the current local second (0–999) on a Date object. The Date mutates in place and returns updated epoch milliseconds.
getMilliseconds() reads the sub-second fraction (0–999). setMilliseconds(ms) writes it. They are the read/write pair for the millisecond hand of the local clock.
setMilliseconds() changes only the 0–999 fraction inside the current second (with overflow rules). getTime() returns the full epoch milliseconds since 1970—use getTime() for elapsed duration.
JavaScript normalizes overflow. 1500 ms becomes 1 second and 500 ms—the seconds field increments and milliseconds become 500.
No. It returns a number (epoch milliseconds). Call each setter on the same Date variable—do not chain on the return value.
setMilliseconds() changes the local sub-second fraction. setUTCMilliseconds() changes the UTC sub-second fraction. Use the one that matches your timezone rules.
Did you know?
setMilliseconds(1500) does not clamp at 999—it adds one second and sets milliseconds to 500. For elapsed time between two instants, subtract getTime() values instead.