JavaScript Date setHours()

Example 1

const d = new Date();
d.setHours(15);
Try it Yourself »

Description

setHours() sets the hour of a date.

setHours() can also set minutes, seconds and milliseconds.

Example 2

Set the time to 15:35:01

const d = new Date();
d.setHours(15, 35, 1);
Try it Yourself »

Example 3

Set the time to 48 hours ago:

const d = new Date();
d.setHours(d.getHours() - 48);
Try it Yourself »

Browser Support

setHours() is an ECMAScript1 (ES1) feature.

ES1 (JavaScript 1997) is fully supported in all browsers:

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes Yes


Syntax

Date.setHours(hour, min, sec, millisec)

Parameters

hour Required. The Hours.

0 to 23, but other values are allowed:

  •  -1 gives the last hour of the previous day
  • 24 gives the first hour of the next day
min Optional. The Minutes.

0 to 59, but other values are allowed:

  •  -1 gives the last minute of the previous hour
  • 60 gives the first minute of the next hour
sec Optional. The seconds.

0 to 59, but other values are allowed:

  •  -1 gives the last second of the previous minute
  • 60 gives the first second of the next minute
millisec Optional. The milliseconds.

0 to 999, but other values are allowed:

  • -1 gives the last millisecond of the previous second
  • 1000 gives the first millisecond of the next second

Return Value

A number.

Number of milliseconds between the date and January 1, 1970 00:00:00 UTC.
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.