Javascript Date getMilliseconds()

Introduction

Javascript Date getMilliseconds() returns the date's milliseconds.

The returned value is a number, between 0 and 999, representing the milliseconds for the given date according to local time.

let today = new Date();
let milliseconds = today.getMilliseconds();
console.log(milliseconds);/*from  w ww  .ja  v  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);//  www .j  a va  2  s  .  com

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

function addZero(x,n) {
  while (x.toString().length < n) {
    x = "0" + x;/*from www  .  j a  v  a 2  s . c o m*/
  }
  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);



PreviousNext

Related