JavaScript Date getMilliseconds() Method

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Milliseconds (0–999)

What You’ll Learn

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

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().

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.

📝 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.

⚡ Quick Reference

GoalCode
Current ms fractionnew Date().getMilliseconds()
Ms from fixed timenew Date(2026, 6, 4, 14, 30, 45, 250).getMilliseconds() → 250
Full epoch millisecondsdate.getTime()
UTC ms fractiondate.getUTCMilliseconds()
Pad to 3 digitsString(ms).padStart(3, "0")
Valid date check!Number.isNaN(date.getTime())

📋 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

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);
Try It Yourself

How It Works

The value changes every millisecond and always stays between 0 and 999. When it would reach 1000, the seconds field rolls over instead.

📈 Practical Patterns

Fixed times, validation, formatting, and duration measurement.

Example 2 — Read Milliseconds from a Specific Time

Build a date at 14:30:45.250 local time and read its components.

JavaScript
const stamp = new Date(2026, 6, 4, 14, 30, 45, 250);

console.log(stamp.getSeconds());       // 45
console.log(stamp.getMilliseconds());  // 250
Try It Yourself

How It Works

The seven-argument constructor sets year through milliseconds in local time. The last argument is the millisecond fraction within the second.

Example 3 — Validate Before Calling getMilliseconds()

Guard against invalid Date objects that yield NaN.

JavaScript
function safeGetMilliseconds(date) {
  if (!(date instanceof Date) || Number.isNaN(date.getTime())) {
    return null;
  }
  return date.getMilliseconds();
}

console.log(safeGetMilliseconds(new Date()));              // e.g. 512
console.log(safeGetMilliseconds(new Date("not-a-date")));  // null
Try It Yourself

How It Works

instanceof Date alone is not enough—invalid dates are still Date objects. Always check getTime() for NaN.

Example 4 — Format Seconds with Milliseconds

Pad the millisecond value to three digits for log-style output.

JavaScript
function formatSecondsWithMs(date) {
  const sec = date.getSeconds();
  const ms = String(date.getMilliseconds()).padStart(3, "0");
  return sec + "." + ms + "s";
}

console.log(formatSecondsWithMs(new Date(2026, 6, 4, 14, 30, 7, 5)));
// "7.005s"
Try It Yourself

How It Works

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");
Try It Yourself

How It Works

getMilliseconds() only reads 0–999 inside one second. Date.now() and getTime() return full epoch milliseconds—the right tool for benchmarking and timers.

🚀 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.

📝 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.

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 Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · 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().

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.

💡 Best Practices

✅ Do

  • Validate dates before calling getMilliseconds()
  • Use getTime() for elapsed duration
  • Pad ms values when formatting logs
  • Pair with getSeconds() for full sub-second stamps
  • Pick UTC getters when storing UTC instants

❌ Don’t

  • Confuse getMilliseconds() with getTime()
  • Expect values outside 0–999
  • Use !isNaN(date) instead of checking getTime()
  • Benchmark long tasks with getMilliseconds() alone
  • Forget timezone context near second boundaries

Key Takeaways

Knowledge Unlocked

Five things to remember about Date.getMilliseconds()

Your foundation for sub-second reads in JavaScript.

5
Core concepts
🔢 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().

Continue to getTime()

Learn how to read the full milliseconds since the Unix epoch from a Date object.

getTime() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful