Javascript Date getHours()
returns the date's hours as a number between 0 and 23.
The returned value is an integer number, between 0 and 23, representing the hour for the given date according to local time.
// current date// w ww. j av a2s. c om let date = new Date(); // the hour in your current time zone console.log( date.getHours() ); let d = new Date('December 25, 2020 23:15:30'); let hours = d.getHours(); console.log(hours); // 23
Using getHours()
, getMinutes()
, and getSeconds()
to display the time:
function addZero(i) { if (i < 10) { i = "0" + i;/* w ww.j a v a 2s. c o m*/ } return i; } var d = new Date(); var h = addZero(d.getHours()); var m = addZero(d.getMinutes()); var s = addZero(d.getSeconds()); console.log(h + ":" + m + ":" + s);