Date.prototype.getMilliseconds()

Return the milliseconds past the second for a date according to local time.

Signature
getMilliseconds()
  • Added in JavaScript ES1

Return value

numberAn integer between 0 and 999 representing the milliseconds for the given date according to local time. Returns NaN if the date is invalid.

Errors

Throws a TypeError if called on a value that is not a Date object.

Changelog

  • PHP —Browsers reduce time precision as a fingerprinting defence, so the value observed from a live clock may be rounded — Firefox to 2 ms by default, and to 100 ms when privacy.resistFingerprinting is enabled.

Returns the milliseconds past the second, 0 through 999, in local time. It is the finest resolution a Date can express — there is no microsecond field, and the underlying time value is a whole number of milliseconds.

When to reach for it

Use it for log timestamps that need sub-second ordering, for ISO-style formatting done by hand, and when combining with getSeconds() to get a fractional second.

What trips people up

It is a field, not an elapsed count. For "milliseconds since the epoch" you want Date.now() or getTime(); getMilliseconds() only ever returns 0 to 999. Calling it on a Date built from a duration is a reliable way to get a number that looks plausible and is wrong.

Padding needs three digits. A value of 7 formats as "007", not "07" — padStart(3, "0"), not 2. Getting the width wrong produces timestamps that sort incorrectly as strings.

Omitted milliseconds default to 0. Dates built from "1995-12-25T23:15:30" or from six constructor arguments all land exactly on the second, so a whole batch of records can share the value 0. That is not a bug, and it means the field is useless as a tiebreaker unless the source actually carried sub-second precision.

Live clock readings are also deliberately coarsened by browsers, so consecutive reads may not differ.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Format a log line as HH:MM:SS.mmm for sub-second ordering
  • Combine with getSeconds() to get a fractional seconds value
  • Build an ISO-like timestamp string by hand
  • Check whether a parsed date carried any sub-second precision

Frequently asked questions

How do I get milliseconds since the epoch?

Use Date.now() or date.getTime(), which return the full millisecond timestamp. getMilliseconds() only returns the 0-999 remainder within the current second.

Why is getMilliseconds() always 0 on my dates?

Because the source did not specify a sub-second value — strings like "2024-03-15T12:00:00" and six-argument constructor calls both default the milliseconds to 0. Only a value that carried a fraction will report anything else.

Can a Date store microseconds?

No. A Date's time value is a whole number of milliseconds, so anything finer is lost. Use performance.now() for fractional-millisecond measurement, or the Temporal API where it is available.

More examples

A log timestamp

The width-3 pad turns 45 into "045" and 7 into "007". With a two-digit pad those lines would read .45 and .07, which sort ahead of .007 as strings.

Example · javascript
function stamp(date) {
  const p = (n, w = 2) => String(n).padStart(w, "0");
  return `${p(date.getHours())}:${p(date.getMinutes())}:` +
         `${p(date.getSeconds())}.${p(date.getMilliseconds(), 3)}`;
}

console.log(stamp(new Date(2024, 0, 5, 9, 7, 3, 45)));
console.log(stamp(new Date(2024, 0, 5, 14, 30, 0, 7)));
console.log(stamp(new Date(2024, 0, 5, 23, 59, 59, 999)));

Fractional seconds

getSeconds() truncates to 45, but adding the milliseconds recovers 45.6 and rounds to 46. The last line rebuilds the offset within the hour in milliseconds.

Example · javascript
const t = new Date(2024, 0, 1, 10, 30, 45, 600);

console.log(t.getSeconds());
console.log(t.getSeconds() + t.getMilliseconds() / 1000);
console.log(Math.round(t.getSeconds() + t.getMilliseconds() / 1000));

// The whole sub-minute part as a single number
console.log(t.getMinutes() * 60_000 + t.getSeconds() * 1000 + t.getMilliseconds());

Did the source carry sub-second precision?

An absent fraction and an explicit .000 produce the identical timestamp, so a value of 0 cannot tell you whether the source had precision. Only a non-zero result, like 250, is evidence.

Example · javascript
const inputs = [
  "2024-03-15T12:00:00Z",
  "2024-03-15T12:00:00.250Z",
  "2024-03-15T12:00:00.000Z",
];

for (const s of inputs) {
  const d = new Date(s);
  console.log(s, "->", d.getUTCMilliseconds());
}
console.log(new Date(inputs[0]).getTime() === new Date(inputs[2]).getTime());

Discussion

  • Be the first to comment on this lesson.