The getHours() method returns the hour ranged from 0 to 23 of the date object.
The getHours() method returns the hour ranged from 0 to 23 of the date object.
Date.getHours()
None
A Number, from 0 to 23, representing the hour
Return the hour, according to local time:
//display the hour of the time right now. var d = new Date(); var n = d.getHours(); console.log(n);/*ww w .ja v a 2 s.com*/ //Return the hour from a specific date and time: //display the hour of a given date-time. var d = new Date("July 21, 2010 01:15:00"); var n = d.getHours(); console.log(n);
Using getHours(), getMinutes(), and getSeconds() to display the time:
//display the time. function addZero(i) { if (i < 10) { i = "0" + i; }//from ww w . jav a 2 s . c om 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);