getHours() returns the hours component of a JavaScript Date according to local time. The normal range is 0 to 23.
const now = new Date();
const value = now.getHours();
console.log(value);
const value = date.getHours();
The method takes no parameters. If the Date is invalid, it returns NaN.
const value = String(date.getHours()).padStart(2, "0");
const now = new Date();
const hh = String(now.getHours()).padStart(2, "0");
const mm = String(now.getMinutes()).padStart(2, "0");
const ss = String(now.getSeconds()).padStart(2, "0");
console.log(`${{hh}}:${{mm}}:${{ss}}`);
For an updating display, see the JavaScript clock and timer tutorial.
const localValue = date.getHours();
const utcValue = date.getUTCHours();
Use the local method for the host's local time and the UTC method when your logic is explicitly based on UTC. See UTC date methods.
const bad = new Date("invalid");
console.log(bad.getHours()); // NaN
const hour24 = date.getHours();
const period = hour24 >= 12 ? "PM" : "AM";
const hour12 = hour24 % 12 || 12;
Date Reference Next Date Method
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.