Latest web development tutorials
 

JavaScript getMilliseconds() Method

< JavaScript Date Object

Example

Return the milliseconds, according to local time:

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

The result of n could be:

Try it Yourself »

More "Try it Yourself" examples below.


Definition and Usage

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


Browser Support

Method
getMilliseconds() Yes Yes Yes Yes Yes

Syntax

Date.getMilliseconds()

Parameters

None

Technical Details

Return Value: A Number, from 0 to 999, representing milliseconds
JavaScript Version: 1.3

Examples

More Examples

Example

Return the milliseconds from a specific date and time:

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

The result of n will be:

526
Try it Yourself »

Example

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

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 »

< JavaScript Date Object