JavaScript getHours()Method

Date 对象参考手册JavaScript Date Object

Example

Return the hour field of the time according to the local time:

var d = new Date();
var n = d.getHours();

nOutput:


Try it Yourself »

Definition and Usage

The getHours() method returns the hour field of the time.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the getHours() method.


Syntax

Date.getHours()

Return Value

Type Description
Number The return value is an integer between 0 (midnight) and 23 (11 PM).

Technical Details

JavaScript Version: 1.0


More Examples

Example

Return the hour field of the specified time:

var d = new Date("July 21, 1983 01:15:00");
var n = d.getHours();

nOutput:

1

Try it Yourself »
实例s

Display the current time
How to use getHours(), getMinutes(), and getSeconds() to display the current time.


Date 对象参考手册JavaScript Date Object

Other Extensions