Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Date.prototype.setHours

Sets the hour, and optionally minutes, seconds and milliseconds.

Available since: ES1

Syntax

date.setHours(hours, minutes, seconds, ms)

Parameters

ParameterTypeDescription
hoursnumberThe hour from 0 to 23.
minutes, seconds, msnumberOptional. Minutes, seconds and milliseconds.

Return value

numberThe new timestamp. The object itself changes.

Examples

The start of the day — midnight:

const date = new Date(2026, 2, 15, 18, 45, 30);
date.setHours(0, 0, 0, 0);
 
console.log(date.getHours(), date.getMinutes(), date.getSeconds());

Output:

0 0 0

Notes

This is handy for comparing dates without time: zero the time on both and compare getTime().

See also

In the tutorial

  • Dates and time — The Date object in JavaScript: creating a date, reading the year, month and day, changing a date, the difference between dates and the ISO format.

Thanks for reading!

Donate to the site — we work hard for you.