skip to content

In Angular Material 22, how do you restrict mat-timepicker to pick-up slots every 15 minutes between 08:00 and 18:00, and when do you use its options input instead?

level: seniorimportance: nice to knowfreq 24%

answer

  1. a dropdown of generated times
  2. spacing set on the timepicker
  3. bounds set on the input
  4. always write the unit
  5. custom value-label list, never both

basics

~10 s

Bind the input with [matTimepicker], set interval="15m" on mat-timepicker and matTimepickerMin="08:00" and matTimepickerMax="18:00" on the input. Use the options input, an array of value-label objects, for irregular slots; setting both interval and options throws.

solid answer

~50 s

`mat-timepicker` is a dropdown attached to an input through `[matTimepicker]`, usually with a `mat-timepicker-toggle`. It generates its options from the `interval` input, 30 minutes by default and configurable app-wide with `MAT_TIMEPICKER_CONFIG`. Write the unit (`15m`, `1.5 hours`): in the 22.2.1 source a bare number is read as seconds, although the guide says minutes. `matTimepickerMin` and `matTimepickerMax` on the input take a time string or a date value, trim the generated list and validate typed times, reporting `matTimepickerMin`, `matTimepickerMax` or `matTimepickerParse`. For irregular slots, such as a branch closed from 12:00 to 13:00, pass `options` as an array of `MatTimepickerOption` objects (`{value, label}`); in development mode, setting both `options` and `interval`, or passing an empty array, throws. The timepicker only changes the time portion of the value, so binding it and a datepicker to one control gives a combined date and time.

code

ts · 22 lines
ts
import {Component} from '@angular/core';
import {MatTimepickerModule, MatTimepickerOption} from '@angular/material/timepicker';
import {DateTime} from 'luxon';

const day = DateTime.now().startOf('day');
const OPEN_HOURS = [8, 9, 10, 11, 13, 14, 15, 16, 17]; // closed 12:00-13:00

@Component({
  selector: 'app-pickup-slot',
  imports: [MatTimepickerModule],
  template: `
    <input [matTimepicker]="slotPicker" aria-label="Pick-up time">
    <mat-timepicker-toggle [for]="slotPicker" />
    <mat-timepicker #slotPicker [options]="slots" />
  `,
})
export class PickupSlot {
  readonly slots: MatTimepickerOption<DateTime>[] = OPEN_HOURS.map(hour => {
    const value = day.set({hour});
    return {value, label: value.toFormat('HH:mm')};
  });
}

go deeper

for a junior

Recall the pieces: an input bound with matTimepicker, a mat-timepicker dropdown, an optional toggle, and the 30-minute default interval.

for a middle

Explain interval strings, the min and max inputs and their errors, and the MAT_DATE_FORMATS time fields the adapter must provide.

for a senior

Show the traps: unitless intervals read as seconds, options and interval throwing together, custom options ignoring bounds, and native time parsing limits.

for a principal

Decide where slot availability comes from, a static interval in the client or a server schedule fed through options, and how stale data is handled at booking.

## The pieces Angular Material's timepicker, added in version 19, follows the datepicker's shape: - **`input[matTimepicker]`**: the text input. It is a value accessor and a validator for `@angular/forms`, and it carries the bounds. - **`mat-timepicker`**: the dropdown of time options, with the `interval` and `options` inputs and a `selected` output. - **`mat-timepicker-toggle`**: an optional clock button whose `for` input names the timepicker. - **The date implementation**: the same `DateAdapter` and `MAT_DATE_FORMATS` the datepicker uses. A time is stored as the time portion of an adapter date value. ## Generating the 15-minute slots ```html <input matInput [matTimepicker]="pickupTime" [formControl]="pickUp" matTimepickerMin="08:00" matTimepickerMax="18:00"> <mat-timepicker-toggle [for]="pickupTime" /> <mat-timepicker #pickupTime interval="15m" /> ``` The timepicker walks from the minimum to the maximum in steps of the interval and labels each option with `display.timeOptionLabel`. Without bounds it covers the whole day, from 00:00 to 23:59. ## Interval strings | Value | Read as | |---|---| | `15m`, `15 min`, `15 minutes` | 15 minutes | | `1.5h`, `1.5 hours` | 90 minutes | | `90s` | 90 seconds | | `15` or the number `15` | **15 seconds** in the 22.2.1 parser | | nothing | 30 minutes, or the `MAT_TIMEPICKER_CONFIG` default | The guide describes a bare number as minutes, but the source's `parseInterval` returns seconds for unitless values and its tests expect `'75'` to mean 75 seconds. `interval="15"` therefore floods the list with about 2,400 entries between 08:00 and 18:00. Intervals below one second are raised to one second so the browser does not freeze. ## Bounds and validation - `matTimepickerMin` and `matTimepickerMax` accept a time string such as `"08:00"` or a date value whose time is used. - They limit the **generated** options and validate typed input, which yields `matTimepickerMin` or `matTimepickerMax` errors. - Text the adapter's `parseTime` cannot read yields `matTimepickerParse`. - If the control has no value yet, a typed or picked time creates today's date with that time. ## Using options instead of interval 1. Reach for `options` when the slots are not evenly spaced: a lunch closure, hand-over windows, or slots loaded from the branch's schedule. 2. Each entry is a `MatTimepickerOption<D>`, an object with `value` (an adapter date) and `label` (the text shown). You build the labels, so format them for the locale yourself. 3. The custom list is shown as given; the min and max inputs do not trim it, though they still validate typed times. 4. In development mode the component throws `Cannot specify both the options and interval inputs at the same time`, and also throws for an empty array. ## What the date adapter must supply - `MAT_DATE_FORMATS` must include `display.timeInput`, `display.timeOptionLabel` and `parse.timeInput`; otherwise development mode throws `MatTimepicker: Incomplete MAT_DATE_FORMATS has been provided`. The four first-party providers' default formats include all three. - The native adapter parses times with a regular expression for hours and minutes separated by a colon or a dot, optionally with seconds and AM/PM, such as `1:45 PM`, `22:45` or `22.45`; other locale styles need the date-fns, Luxon or Moment adapter. - Bind the timepicker and a datepicker to the same form control and the datepicker sets the whole date while the timepicker changes only the time, giving one pick-up date-time value.

  • How do you build one pick-up date-and-time field from the datepicker and the timepicker?
    Bind both inputs to the same form control. The datepicker writes the whole adapter date when a day is chosen, and the timepicker only sets the hours and minutes on that value. If the timepicker is used first, it creates today's date with the chosen time, which the datepicker then replaces.
  • Why would a timepicker on the native adapter reject a pick-up time typed as 14 h 30?
    The native adapter's `parseTime` ignores `parse.timeInput` and uses a regular expression for hours and minutes separated by a colon or a dot, optionally with seconds and AM/PM, such as `1:45 PM` or `22.45`. Styles outside it, like `14 h 30`, fail with `matTimepickerParse`. The date-fns, Luxon and Moment adapters parse with `parse.timeInput` in the locale instead.

  • interval="15" on mat-timepicker always means 15 minutes.
  • Setting interval and options together merges generated and custom slots.
  • matTimepickerMin and matTimepickerMax also trim a custom options list.
  • The timepicker needs no MAT_DATE_FORMATS fields beyond the datepicker's.
  • The timepicker stores a separate time string next to the date value.

Assumes Angular Material 22.2; mat-timepicker was added in 19.0 and does not exist in older majors.