JavaScript Date setMilliseconds() Method

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

What You’ll Learn

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

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.

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

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

⚡ Quick Reference

GoalCode
Set ms to 500date.setMilliseconds(500)
Zero sub-secondsdate.setMilliseconds(0)
Full time with msdate.setHours(12, 30, 45, 250)
Read ms after setdate.getMilliseconds()
Elapsed durationend.getTime() - start.getTime()
Clone before mutateconst copy = new Date(date)

📋 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

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

How It Works

Milliseconds are the fraction inside the current second—not total ms since 1970.

📈 Practical Patterns

Zero noise, combined setters, return values, and overflow.

Example 2 — Zero Out Sub-Second Precision

Round a timestamp down to whole seconds in local time.

JavaScript
const date = new Date(2026, 2, 1, 12, 30, 45, 789);
date.setMilliseconds(0);

console.log(date.getSeconds());       // 45
console.log(date.getMilliseconds());  // 0
Try It Yourself

How It Works

Useful before comparing second-granularity times or displaying HH:MM:SS without fractions.

Example 3 — Set Milliseconds via setHours(h, m, s, ms)

When you need the full local time including ms, the four-argument overload is often clearer.

JavaScript
const date = new Date(2026, 2, 1, 9, 0, 0, 0);
date.setHours(12, 30, 45, 250);

console.log(date.getHours());         // 12
console.log(date.getMilliseconds()); // 250
Try It Yourself

How It Works

setHours() accepts ms as its fourth argument when you want one coordinated update.

Example 4 — Clone, Then Set (Immutable Style)

Preserve the original Date by copying before mutating.

JavaScript
const original = new Date(2026, 2, 1, 12, 30, 45, 100);
const adjusted = new Date(original);
adjusted.setMilliseconds(750);

console.log("Original ms:", original.getMilliseconds());  // 100
console.log("Adjusted ms:", adjusted.getMilliseconds());  // 750
Try It Yourself

How It Works

Setters always mutate their target. Clone first when other code still needs the old instant.

Example 5 — Millisecond Overflow (setMilliseconds(1500))

Values above 999 roll into the next second.

JavaScript
const date = new Date(2026, 2, 1, 12, 30, 45, 0);
date.setMilliseconds(1500);

console.log(date.getSeconds());       // 46
console.log(date.getMilliseconds());  // 500
Try It Yourself

How It Works

1500 ms = 1 second + 500 ms. The engine normalizes instead of clamping to 999.

🚀 Common Use Cases

  • API timestamps — apply fractional seconds from ISO strings.
  • Second roundingsetMilliseconds(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.

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

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

Bottom line: Safe everywhere. Remember sub-second range, mutation, and getTime() for duration.

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.

Next, learn setMinutes() to change the minute component, or review getMilliseconds() for reading sub-seconds.

💡 Best Practices

✅ Do

  • Use setMilliseconds(0) to drop sub-second noise
  • Clone before mutating shared Date objects
  • Use setHours(h, m, s, ms) for full time snaps
  • Pair with getMilliseconds() to verify results
  • Use getTime() for elapsed measurements

❌ Don’t

  • Confuse with epoch milliseconds from getTime()
  • Chain setters on the numeric return value
  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about Date.setMilliseconds()

Your foundation for local sub-second writes in JavaScript.

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

Continue to setMinutes()

Learn how to set the minute component (0–59) on a Date object in local time.

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