Home  >  Article  >  Web Front-end  >  JavaScript method setHours() to set the hour field of a specified time

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

黄舟
黄舟Original
2017-11-07 13:05:433268browse

Definition and Usage

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

Syntax

dateObject.setHours(hour,min,sec,millisec)
Parameters Description
hour Required. A value representing the hour, between 0 (midnight) ~ 23 (11 p.m.), in local time (the same below)
min Optional . A value representing minutes, between 0 and 59. Before EMCAScript was standardized, this parameter was not supported.
sec Optional. A value representing seconds, between 0 and 59. Before EMCAScript was standardized, this parameter was not supported.
millisec Optional. 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>

Output:

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

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>

Output:

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


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!

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