Home > Web Front-end > JS Tutorial > body text

JavaScript method setHours() to set the hour field of a specified time

黄舟
Release: 2017-11-07 13:05:43
Original
3322 people have browsed it

Definition and Usage

The setHours() method is used to set the hour field of the specified time.

Syntax

dateObject.setHours(hour,min,sec,millisec)
Copy after login
ParametersDescription
hour Required. A value representing the hour, between 0 (midnight) ~ 23 (11 p.m.), in local time (the same below)
minOptional . A value representing minutes, between 0 and 59. Before EMCAScript was standardized, this parameter was not supported.
secOptional. A value representing seconds, between 0 and 59. Before EMCAScript was standardized, this parameter was not supported.
millisecOptional. A value representing milliseconds, between 0 and 999. Before EMCAScript was standardized, this parameter was not supported.

Return value

Adjusted date expressed in milliseconds. Before ECMAScript was standardized, this method returned nothing.

Tips and Notes:

Note: If one of the above parameters is specified with a single digit, JavaScript will add one or two leading 0s to the result.

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

Example

Example 1

In this example, we will set the hour field of the current time to 15 through the setHours() method:

<script type="text/javascript">

var d = new Date()
d.setHours(15)
document.write(d)

</script>
Copy after login

Output:

Tue Nov 07 2017 15:46:45 GMT+0800 (中国标准时间)
Copy after login

Example 2

In this example, we will set the time to 15:35:01 through the setHours() method:

<script type="text/javascript">

var d = new Date()
d.setHours(15,35,1)
document.write(d)

</script>
Copy after login

Output:

Tue Nov 07 2017 15:35:01 GMT+0800 (中国标准时间)
Copy after login


The above is the detailed content of JavaScript method setHours() to set the hour field of a specified time. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template