JavaScript Date setHours()

Posted by

Method

JavaScript Date setHours() Method

The setHours() method of the JavaScript Date object is used to set the hour value of the date object. The setHours() method takes a single argument, which is an integer representing the hour of the day. This method can also take two arguments, the first being the hour and the second being the minute.

Syntax

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

dateObj.setHours(hourValue[, minutesValue[, secondsValue[, msValue]]])

[/dm_code_snippet]

Parameters: hourValue (Required) – An integer representing the hour of the day. The valid range of hour values is 0 to 23.

minutesValue (Optional) – An integer representing the minute of the hour. The valid range of minute values is 0 to 59.

secondsValue (Optional) – An integer representing the second of the minute. The valid range of second values is 0 to 59.

msValue (Optional) – An integer representing the millisecond of the second. The valid range of millisecond values is 0 to 999.

Return Value

Returns the number of milliseconds since January 1, 1970 00:00:00 UTC, with the new hour and other values set.

Examples

The following example sets the hour of the date object to 15 (3PM):

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

var date = new Date();
date.setHours(15);

[/dm_code_snippet]

The following example sets the hour and minute of the date object to 15:45:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

var date = new Date();
date.setHours(15, 45);

[/dm_code_snippet]

The following example sets the hour, minute and second of the date object to 15:45:30:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

var date = new Date();
date.setHours(15, 45, 30);

[/dm_code_snippet]

The following example sets the hour, minute, second and millisecond of the date object to 15:45:30:1000:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

var date = new Date();
date.setHours(15, 45, 30, 1000);

[/dm_code_snippet]

The following example sets the hour to 15 (3PM) and the minute to 45, and prints the date object to the console:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

var date = new Date();
date.setHours(15, 45);
console.log(date); // Mon Aug 17 2020 15:45:00 GMT-0500 (Central Daylight Time)

[/dm_code_snippet]