JavaScript Date getMilliseconds() Method

Definition and Usage

The getMilliseconds() method returns the milliseconds for a specified date and time (from 0 to 999).

Example

Example 1

Return milliseconds based on local time:

var d = new Date();
var n = d.getMilliseconds();

Try It Yourself

Example 2

Example

Return the milliseconds for a specific date and time:

var d = new Date("July 21, 1983 01:15:00:526");
var n = d.getMilliseconds();

Try It Yourself

Example 3

Display time (in milliseconds) using getHours(), getMinutes(), getSeconds(), and getMilliseconds():

function addZero(x, n) {
  while (x.toString().length < n) {
    x = "0" + x;
  }
  return x;
}
function myFunction() {
  var d = new Date();
  var x = document.getElementById("demo");
  var h = addZero(d.getHours(), 2);
  var m = addZero(d.getMinutes(), 2);
  var s = addZero(d.getSeconds(), 2);
  var ms = addZero(d.getMilliseconds(), 3);
  x.innerHTML = h + ":" + m + ":" + s + ":" + ms;
}

Try It Yourself

Syntax

Date.getMilliseconds()

Parameters

No parameters.

Technical Details

Return Value: A number, from 0 to 999, representing milliseconds.
JavaScript Version: ECMAScript 1

Browser Support

Method Chrome IE Firefox Safari Opera
getMilliseconds() Support Support Support Support Support

Related Pages

Tutorial:JavaScript Date

Tutorial:JavaScript Date Format

Tutorial:JavaScript Date Retrieval Methods