Date.prototype.setMinutes()

Sets the minutes, and optionally seconds and milliseconds, of a date in local time.

Signature
setMinutes(minutesValue)
setMinutes(minutesValue, secondsValue)
setMinutes(minutesValue, secondsValue, msValue)
  • Added in JavaScript ES1

Parameters

ParameterTypeDescription
$minutesValuenumberAn integer between 0 and 59 representing the minutes. Values outside the range shift the hours and date accordingly.
$secondsValueoptionalnumber= this.getSeconds()An integer between 0 and 59 representing the seconds. Defaults to the value returned by getSeconds().
$msValueoptionalnumber= this.getMilliseconds()An integer between 0 and 999 representing the milliseconds. If specified, secondsValue must be specified too. Defaults to the value returned by getMilliseconds().

Return value

numberThe new timestamp of the modified Date object in milliseconds since the epoch. Returns NaN if any parameter is NaN or coerces to NaN, in which case the Date is set to Invalid Date.

Changelog

  • PHP Modern enginesLocal times that fall in a DST gap or ambiguity resolve like Temporal's "compatible" disambiguation: the earlier instant when two exist, and gaps are crossed forward.

setMinutes() writes the minutes onto an existing date in place, optionally taking seconds and milliseconds in the same call, and returns the new timestamp.

When to reach for it

  • Rounding a timestamp down to a slot boundary — quarter hours, half hours — by computing the target minute and zeroing what follows.
  • Adding minutes: d.setMinutes(d.getMinutes() + 45) crosses hour, day and year boundaries with no bounds checking.
  • Stripping sub-minute precision before displaying or storing a time.

What trips people up

Like the other time setters, the arguments you omit are preserved. setMinutes(30) on a 10:15:42.318 date gives 10:30:42.318 — the seconds and milliseconds ride along and will quietly break an equality comparison later. Pass setMinutes(30, 0, 0) when you want a clean boundary.

Out-of-range values cascade upward instead of throwing. setMinutes(75) becomes 01:15 of the following hour; setMinutes(45, 100) resolves to minute 46, second 40 — the excess seconds are folded in after the minutes are applied.

This is a local-time operation. When the result must be identical regardless of the machine's zone, use setUTCMinutes().

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Round a booking time down to the nearest 15-minute slot
  • Add a fixed number of minutes to a deadline
  • Strip seconds and milliseconds before comparing two times
  • Snap a scheduled job to the top or bottom of the hour

Frequently asked questions

How do I round a time to the nearest 15 minutes in JavaScript?

Compute the target minute with Math.round or Math.floor on getMinutes() / 15, multiply back by 15, and pass it to setMinutes along with 0 for seconds and milliseconds. Omitting those two arguments leaves stray precision that breaks later comparisons.

Does setMinutes() reset the seconds?

No — omitted arguments keep their current values, so the existing seconds and milliseconds survive. Call setMinutes(m, 0, 0) explicitly if you want them cleared.

What happens if I pass a value over 59 to setMinutes()?

The excess rolls into the hours and, if needed, the date — setMinutes(75) lands on minute 15 of the following hour. Nothing throws, which is what makes d.setMinutes(d.getMinutes() + n) a safe way to add minutes.

More examples

Round down to a slot boundary

Zeroing seconds and milliseconds in the same call is what makes the results comparable — without those two arguments each slot would still carry :51.900 and no two rounded times would ever be equal.

Example · javascript
function floorToSlot(date, minutes) {
  const out = new Date(date);
  out.setMinutes(Math.floor(out.getMinutes() / minutes) * minutes, 0, 0);
  return out;
}

const t = new Date(2024, 5, 10, 14, 37, 51, 900);
console.log(floorToSlot(t, 15).toTimeString().slice(0, 8)); // 14:30:00
console.log(floorToSlot(t, 30).toTimeString().slice(0, 8)); // 14:30:00
console.log(floorToSlot(t, 5).toTimeString().slice(0, 8));  // 14:35:00

Adding minutes across boundaries

Adding 45 minutes to 23:30 on New Year's Eve rolls the date into 1 January 2025 without any manual carry. The second case shows overflow resolving after the minutes are set: 45 minutes plus 100 seconds becomes 46:40.

Example · javascript
const meeting = new Date(2024, 11, 31, 23, 30, 0);
meeting.setMinutes(meeting.getMinutes() + 45);
console.log(meeting.toDateString(), meeting.toTimeString().slice(0, 5));

const t = new Date(2024, 0, 1, 10, 30, 0, 0);
t.setMinutes(45, 100, 500); // 100 seconds overflows
console.log(t.getMinutes(), t.getSeconds(), t.getMilliseconds()); // 46 40 500

Discussion

  • Be the first to comment on this lesson.