Javascript - Date getMilliseconds() Method

The getMilliseconds() method returns the milliseconds ranged from 0 to 999 of the date object.

Description

The getMilliseconds() method returns the milliseconds ranged from 0 to 999 of the date object.

Syntax

Date.getMilliseconds()

Parameters

None

Return

A Number, from 0 to 999, representing milliseconds

Return the milliseconds, according to local time:

Demo

//display the milliseconds of the time right now.
var d = new Date();
var n = d.getMilliseconds();
console.log(n);/*from w  w  w  . j  av a 2  s  . c  o m*/

//Return the milliseconds from a specific date and time:
var d = new Date("July 21, 2010 01:15:00:526");
var n = d.getMilliseconds();
console.log(n);

Result

Using getHours(), getMinutes(), getSeconds(), and getMilliseconds() to display the time (with milliseconds):

Demo

//display the time.
function addZero(x,n) {
    while (x.toString().length < n) {
        x = "0" + x;
    }//from   w ww . j  a v a  2 s . com
    return x;
}

var d = new Date();
var h = addZero(d.getHours(), 2);
var m = addZero(d.getMinutes(), 2);
var s = addZero(d.getSeconds(), 2);
var ms = addZero(d.getMilliseconds(), 3);
console.log( h + ":" + m + ":" + s + ":" + ms);

Result