Home >Web Front-end >JS Tutorial >JavaScript method getHours() that returns the hour field of time

JavaScript method getHours() that returns the hour field of time

黄舟
黄舟Original
2017-11-06 09:58:422800browse

Definition and Usage

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

Syntax

dateObject.getHours()

Return value

The hour field of dateObject, displayed in local time. The return value is an integer between 0 (midnight) and 23 (11pm).

Tips and Notes:

Note: The value returned by getHours() is a two-digit number. However, the return value is not always two digits, if the value is less than 10, only one digit is returned.

Note: This method is always used in conjunction with a Date object.

Example

Example 1

In this example, we can get the hour of the current time:

<script type="text/javascript">

var d = new Date()
document.write(d.getHours())

</script>

Output:

9

Example 2

In this example, we will extract the hours field from the specific date and time:

<script type="text/javascript">

var born = new Date("July 21, 1983 01:15:00")
document.write(born.getHours())

</script>

Output:

1

javascript Date.getHours() method Returns the hour on the specified date in local time. The value returned by getHours is an integer between 0 and 23.

Returns the hour of the specified date according to local time.
Example:

<html>
<head>
<title>JavaScript getHours Method</title>
</head>
<body>
<script type="text/javascript">
  var dt = new Date("December 25, 1995 23:15:00");
  document.write("getHours() : " + dt.getHours() ); 
</script>
</body>
</html>

This will produce the following results:

getHours() : 23


The above is the detailed content of JavaScript method getHours() that returns the hour field of time. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn