Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript setHours() method

JavaScript setHours() method

Date Object Reference JavaScript Date object

Example

Set the time field to 15:

var d = new Date();

d.setHours(15);

d Output result:

Try it »


Definition and Usage

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

This method can also be used to set the minutes, seconds, and milliseconds.


Browser support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the setHours() method


Syntax

Date

.setHours(

hour,min,sec,millisec

)

Parameter value

Parameters Description
hour Required.

A value for the hour, between 0 (midnight) and 23 (11 PM), in local time (same below).

  • -1 is the last hour of the previous day
  • 24 is the first hour of the next day
  • | | *min* | Optional. A value representing the minutes.

    , between 0 and 59. Before ECMAScript standardization, this parameter was not supported:

    | | *sec* | Optional. A value representing the seconds

    , between 0 and 59. Before ECMAScript standardization, this parameter was not supported:

    | | *millisec* | Optional. A value representing the milliseconds.

    Between 0 and 999. Before ECMAScript standardization, this parameter was not supported:

    |

    Return Value

    Type Description
    Number Milliseconds between the date object and midnight January 1 1970

    Technical Details

    JavaScript version: 1.0

    More examples

    Example

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

    var d = new Date();

    d.setHours(15,35,1);

    d Output result:

    Try it »

    Example

    Set the time to 48 hours ago:

    var d = new Date();

    d.setHours(d.getHours()-48);

    d Output result:

    Try it »


    Date Object Reference JavaScript Date object