Javascript - Date getUTCMilliseconds() Method

The getUTCMilliseconds() method returns the milliseconds ranged from 0 to 999 from the date object, according to universal time.

Description

The getUTCMilliseconds() method returns the milliseconds ranged from 0 to 999 from the date object, according to universal time.

UTC time is the same as GMT time.

Syntax

Date.getUTCMilliseconds()

Parameters

None

Return

A Number, from 0-999, representing milliseconds

Example

Return the milliseconds, according to UTC:

Demo

//display milliseconds, according to UTC.
var d = new Date();
var n = d.getUTCMilliseconds();
console.log(n);// w w w .  j a  v a2  s .c om

//Return the UTC milliseconds from a specific date and time:

//display milliseconds of a specific date-time, according to UTC.
var d = new Date("July 21, 2010 01:15:00:195");
var n = d.getUTCMilliseconds();
console.log(n);

Result

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

Demo

//display the UTC time.
function addZero(x,n) {
    while (x.toString().length < n) {
        x = "0" + x;
    }/*from w  ww  .java2s  . com*/
    return x;
}

var d = new Date();
var h = addZero(d.getUTCHours(), 2);
var m = addZero(d.getUTCMinutes(), 2);
var s = addZero(d.getUTCSeconds(), 2);
var ms = addZero(d.getUTCMilliseconds(), 3);
console.log( h + ":" + m + ":" + s + ":" + ms);

Result