getMilliseconds() returns the millisecond fraction within the current second as an integer from 0 to 999 in local time. This guide covers syntax, five examples, comparisons with getTime(), validation, and formatting patterns.
01
Syntax
date.getMilliseconds()
02
Range
0 – 999
03
Sub-second
Not epoch ms
04
vs getTime
Different jobs
05
Format
Pad to 3 digits
06
Validate
Invalid → NaN
Fundamentals
Introduction
Timestamps, logs, and animations sometimes need sub-second precision. The getMilliseconds() method reads the millisecond part of a Date object—the value after the decimal point inside the current second.
Do not confuse it with getTime(), which returns the full milliseconds since January 1, 1970 UTC. For elapsed duration between two moments, subtract getTime() values or use Date.now().
Concept
Understanding the getMilliseconds() Method
Date.prototype.getMilliseconds() is a zero-argument instance method. It never mutates the original Date—it only reads the millisecond component (0–999) within the current second in local time.
Think of a clock showing 14:30:45.250. getSeconds() returns 45 and getMilliseconds() returns 250—not the entire timestamp as one number.
💡
Beginner Tip
getMilliseconds() answers “where inside this second?” getTime() answers “how many ms since 1970?” They solve different problems.
Foundation
📝 Syntax
JavaScript
dateObj.getMilliseconds()
Parameters
None.
Return value
Integer 0–999 for a valid date in local time.
NaN if dateObj is an invalid Date.
Cheat Sheet
⚡ Quick Reference
Goal
Code
Current ms fraction
new Date().getMilliseconds()
Ms from fixed time
new Date(2026, 6, 4, 14, 30, 45, 250).getMilliseconds() → 250
Full epoch milliseconds
date.getTime()
UTC ms fraction
date.getUTCMilliseconds()
Pad to 3 digits
String(ms).padStart(3, "0")
Valid date check
!Number.isNaN(date.getTime())
Compare
📋 getMilliseconds() vs Similar Methods
These APIs all mention “milliseconds” but return very different numbers. Pick the one that matches your task.
getMilliseconds()
0–999
Sub-second fraction
getTime()
epoch ms
Full instant
Date.now()
epoch ms
Static shortcut
getSeconds()
0–59
Whole seconds
Hands-On
Examples Gallery
Open DevTools Console (F12) or use Try-it links. Fixed times use new Date(y, m, d, h, min, sec, ms) for predictable local values.
📚 Getting Started
Read the millisecond fraction from the current time.
Example 1 — Get the Current Millisecond Fraction
Call getMilliseconds() on a new Date() to read the sub-second part right now.
JavaScript
const now = new Date();
const ms = now.getMilliseconds();
console.log("Milliseconds:", ms);
padStart(3, "0") turns 5 into "005" so the fractional part keeps a fixed width in logs and UI.
Example 5 — Measure Elapsed Time (Use getTime(), Not getMilliseconds())
For duration between two moments, subtract epoch milliseconds.
JavaScript
const start = Date.now();
// Simulate work
let sum = 0;
for (let i = 0; i < 100000; i++) {
sum += i;
}
const elapsed = Date.now() - start;
console.log("Elapsed:", elapsed, "ms");
getMilliseconds() only reads 0–999 inside one second. Date.now() and getTime() return full epoch milliseconds—the right tool for benchmarking and timers.
Applications
🚀 Common Use Cases
Log timestamps — append .ms fractions to second-level logs.
UI clocks — show tenths or milliseconds in a stopwatch display.
Event ordering — break ties when two events share the same second.
Testing — assert sub-second fields on constructed dates.
Media sync — align cues within a second boundary.
Debugging — inspect the exact ms slot on a parsed date.
🧠 How getMilliseconds() Resolves the Value
1
Internal instant
The Date stores milliseconds since Unix epoch (UTC).
Storage
2
Local conversion
Engine applies the runtime timezone offset.
TZ
3
Extract ms fraction
Remainder within the current second: 0–999.
getMilliseconds
4
⏱
Use or format
Pad, log, compare, or pass to setMilliseconds.
Important
📝 Notes
getMilliseconds() is local; use getUTCMilliseconds() for UTC.
Range is 0–999 only—not total epoch milliseconds.
For elapsed duration, use getTime() or Date.now().
Invalid dates propagate NaN—validate first.
Pad with padStart(3, "0") when displaying fixed-width fractions.
For high-precision benchmarks, consider performance.now() in browsers.
Compatibility
Browser & Runtime Support
Date.prototype.getMilliseconds() has been available since the first JavaScript Date implementation (ES1). It works in every browser and Node.js.
✓ Baseline · ES1
Date.prototype.getMilliseconds()
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.getMilliseconds()Excellent
Bottom line: Safe everywhere. Remember: sub-second fraction (0–999) is not the same as epoch milliseconds from getTime().
Wrap Up
Conclusion
getMilliseconds() reads the 0–999 fraction inside the current second in local time. Use it for sub-second display and inspection; use getTime() or Date.now() when you need elapsed duration.
Next, learn getTime() for full epoch milliseconds, or getSeconds() for the whole-second component.
Five things to remember about Date.getMilliseconds()
Your foundation for sub-second reads in JavaScript.
5
Core concepts
📝01
Syntax
date.getMilliseconds()
API
🔢02
0–999
Sub-second.
Range
🌐03
Local
Not UTC.
Timezone
⏱04
Not getTime
Epoch vs fraction.
Pitfall
📝05
Pad 3
padStart.
Format
❓ Frequently Asked Questions
An integer from 0 to 999 representing the milliseconds within the current second of the Date object's local time—not the total milliseconds since 1970.
getMilliseconds() returns 0–999 for the sub-second fraction of that instant. getTime() returns the full count of milliseconds since the Unix epoch (January 1, 1970 UTC).
getMilliseconds() uses local timezone rules. getUTCMilliseconds() reads the UTC millisecond fraction. They usually match but can differ near timezone boundaries in edge cases.
NaN. Validate with !Number.isNaN(date.getTime()) before using the result.
For durations between two moments, subtract getTime() values or use Date.now() and performance.now(). getMilliseconds() only reads the 0–999 part inside one second.
No. The range is 0–999. When milliseconds overflow, JavaScript carries into the seconds field automatically.
Did you know?
Subtracting two Date objects (e.g. end - start) coerces them to epoch milliseconds via getTime()—not via getMilliseconds(). That is why elapsed-time recipes use Date.now() or getTime().