JavaScript Date setMilliseconds() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Date

Definition

Date.prototype.setMilliseconds() changes the milliseconds of a date in local time (typically 0–999). It mutates the Date in place and returns the new timestamp.

Remember
date.setMilliseconds(ms)

ms: usually 0 … 999   (local)
→ returns new timestamp (ms)
→ mutates date

setMilliseconds(456)   // exact fraction
setMilliseconds(1005)  // overflow → +1 second, ms = 5

getMilliseconds() → read twin

Syntax

Call date.setMilliseconds. Start with this line.

JavaScript
date.setMilliseconds(ms)
CallWhat it does
date.setMilliseconds(ms)Primary signature.
date.setMilliseconds(...)Call with the arguments your case needs.

Built into JavaScript. Call setMilliseconds(...) on a Date instance — no import required.

Library declarations
JavaScript
date.setMilliseconds(ms)

Parameters

ParameterDescription
millisecondsValueRequired. Local milliseconds (typically 0–999; overflow allowed)

Return Value

Return ValueDescription
*New epoch ms (number), not the Date

JavaScript Examples

Example 1 — Set the Milliseconds

Write a value in the normal 0–999 range.

JavaScript
const event = new Date(1975, 7, 19, 23, 15, 30);

console.log(event.getMilliseconds()); // 0

event.setMilliseconds(456);

console.log(event.getMilliseconds()); // 456
Try It Yourself

How It Works

  1. Start at 23:15:30. Constructor defaults milliseconds to 0.
  2. Read first. getMilliseconds() confirms 0.
  3. Write 456. setMilliseconds(456) updates only the fraction.
  4. Read again. getMilliseconds() now returns 456.

Example 2 — Overflow into Seconds

Values above 999 roll into the seconds field.

JavaScript
const d = new Date(2026, 2, 15, 10, 0, 30, 0);

d.setMilliseconds(1005);

console.log(d.getSeconds());      // 31
console.log(d.getMilliseconds()); // 5
Try It Yourself

How It Works

  1. Start at :30.000. Seconds are 30, milliseconds 0.
  2. Ask for 1005 ms. That is more than one second.
  3. Engine normalizes. 1005 = 1000 + 5 → add 1 second, keep 5 ms.
  4. Read back. Seconds become 31; milliseconds become 5.

Example 3 — Snap Milliseconds to Zero

Clear the fraction so the timestamp lands on a whole second.

JavaScript
const d = new Date(2026, 2, 15, 10, 0, 30, 789);

d.setMilliseconds(0);

console.log(d.getMilliseconds()); // 0
console.log(d.getSeconds());      // 30
Try It Yourself

How It Works

  1. Start with a fraction. Milliseconds are 789.
  2. Clear it. setMilliseconds(0) writes zero.
  3. Seconds stay. The second field remains 30.
  4. Why it helps. Useful when you want clean whole-second timestamps for display or grouping.

Browser & Runtime Support

Date.prototype.setMilliseconds() has been in JavaScript since ES1. It is Baseline and works in every modern browser and Node.js — no polyfill needed.

✓ Baseline · ES1

Date.prototype.setMilliseconds()

Supported everywhere Date exists: Chrome, Firefox, Safari, Edge, Internet Explorer, and all Node.js versions.

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 to use in any environment. Watch mutation and overflow into seconds. See also the MDN setMilliseconds() reference.

Frequently Asked Questions

It sets the milliseconds within the current local second (typically 0–999). The Date mutates in place and the method returns the new timestamp in milliseconds.
getMilliseconds() reads the sub-second fraction (0–999). setMilliseconds(ms) writes it. They are the read/write pair for local milliseconds.
JavaScript normalizes overflow. 1005 ms becomes 1 second and 5 ms — the seconds field increments and milliseconds become 5.
No. It returns a number (epoch milliseconds). Call setters on the 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.
Use setTime() when you need a fixed elapsed-time adjustment in absolute milliseconds. Prefer setMilliseconds() when you only want to change the 0–999 fraction of the current second.

Next: setMinutes()

Learn how to set the minutes portion of a Date 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.

11 people found this page helpful