Date.prototype.getHours
Returns the hour (0-23) for the specified date according to local time
Syntax
date.getHours()Return Value
An integer from 0 to 23
Examples
const date = new Date('2024-06-15T14:30:00');
console.log(date.getHours()); // 14function greeting() {
const hour = new Date().getHours();
if (hour < 12) return 'Good morning';
if (hour < 18) return 'Good afternoon';
return 'Good evening';
}const date = new Date();
const period = date.getHours() >= 12 ? 'PM' : 'AM';
console.log(period);Understanding Date.prototype.getHours
The Date.prototype.getHours method in JavaScript returns the hour (0-23) for the specified date according to local time. It belongs to the Date object and is one of the most widely used methods for working with date values in modern JavaScript and TypeScript applications.
The method signature is date.getHours(). When called, it returns an integer from 0 to 23. Understanding when and how to use getHours() helps you write more expressive, readable code.
Common use cases for Date.prototype.getHours include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like date-getminutes, date-getseconds, date-sethours, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Date.prototype.getHours is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
More Date Methods
Other methods in the Date object
Related Tools
More Date Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.