dentil

Date Picker

Pick a date with SingleDatePicker or a range with DateRangePicker. Both are controlled, return Date objects or unix seconds, and add presets, shortcuts and billing cycles beside the stock shadcn calendar.

@dentil/date-picker
On this page
Result: —

Installation

pnpm dlx shadcn@latest add @dentil/date-picker
npx shadcn@latest add @dentil/date-picker
yarn dlx shadcn@latest add @dentil/date-picker
bunx --bun shadcn@latest add @dentil/date-picker

Files land in components/dentil/date-picker/. Uses the stock shadcn button, calendar, input and popover, and date-fns. If your CLI can't resolve @dentil, install by URL: https://dentil.dev/r/date-picker.json.

Usage

A range only reaches onChange once both ends are picked. Clicking a day when a full range is already selected starts a new one.

'use client';

import { useState } from 'react';

import { DateRangePicker, SingleDatePicker } from '@/components/dentil/date-picker/date-picker';

export function ReportFilters() {
  const [due, setDue] = useState<Date>();
  const [period, setPeriod] = useState<{ start: Date | undefined; end: Date | undefined }>();

  return (
    <>
      <SingleDatePicker value={due} onChange={setDue} minDate={new Date()} />
      <DateRangePicker value={period} onChange={setPeriod} showQuickSelect />
    </>
  );
}

Examples

Single date with limits

Days outside the next 90 days are disabled, and the shortcuts beside the calendar respect the same limits.

Result: —

Billing cycles

Each cycle picks the last second before the next period starts. Type a number to pick any cycle count.

Result: —

Apply step

Pick a range, then close the popover without applying: onChange isn't called and the selection resets.

onChange calls: 0

Unix timestamps

With returnFormat="timestamp", value and onChange use unix seconds, ready for an API.

Result: —

Custom presets

The defaults are exported as quickSelectOptions and defaultSingleDateShortcuts, so you can extend them rather than starting over.

import { endOfDay, startOfDay, subDays } from 'date-fns';

import { DateRangePicker, quickSelectOptions, type QuickSelectOption } from '@/components/dentil/date-picker/date-picker';

const presets: QuickSelectOption[] = [
  {
    label: 'Last 30 days',
    value: 'last_30_days',
    getRange: () => ({ start: startOfDay(subDays(new Date(), 29)), end: endOfDay(new Date()) }),
  },
  ...quickSelectOptions.filter((option) => option.value !== 'last_4_weeks'),
];

<DateRangePicker value={period} onChange={setPeriod} showQuickSelect presets={presets} />;

API reference

DatePicker

SingleDatePicker and DateRangePicker take the same props without mode.

PropTypeDefaultDescription
mode'single' | 'range'—Required on DatePicker. SingleDatePicker and DateRangePicker set it for you.
returnFormat'date' | 'timestamp''date''date' reads and writes Date objects. 'timestamp' reads and writes unix seconds.
valueDate | number | { start, end } | null | undefined—A single value in single mode, or { start, end } in range mode, in the returnFormat type.
onChange(value) => void—Called once a date or a full range is picked, or on Apply with showApplyButton. Called with undefined when cleared.
placeholderstring'Select a date' | 'Select a date range'Trigger label when there is no value.
dateFormatstring'd MMM yyyy'A date-fns format string for the trigger label and cycle dates.
classNamestring—Applied to the root, which wraps the trigger and clear button. Set the width here.
disabledbooleanfalseDisables the trigger and every day.
showClearbooleantrueShow a clear button inside the trigger when there is a value.
align'start' | 'center' | 'end''start'Popover alignment.
buttonSize'sm' | 'default''default'Trigger button size.
endOfDaybooleantrueMove a range’s end to 23:59:59.999 of its day, so the range includes the whole last day.
showQuickSelectbooleanfalseRange mode: show presets beside the calendar.
presetsQuickSelectOption[]quickSelectOptionsRange presets: last 7 days, 4 weeks, 6 and 12 months, and month, quarter and year to date.
showShortcutsbooleanfalseSingle mode: show shortcuts and cycleConfig cycles beside the calendar.
shortcutsSingleDateShortcut[]defaultSingleDateShortcutsToday and the 1st of next month. Pass [] to show only cycles.
cycleConfig{ startDate: Date | number; durationMonths?: number }—Adds 1, 3, 4, 6 and 12 cycle buttons and a custom count. Each picks the last second of that cycle, so 1 cycle from 1 Mar is 31 Mar 23:59:59. durationMonths defaults to 1.
minDateDate | number—Days before it are disabled, and shortcuts and cycles before it are greyed out.
maxDateDate | number—Days after it are disabled.
defaultMonthDate | number—Month shown on open. Defaults to the selected date, then minDate, then today.
showApplyButtonbooleanfalseHold the selection until Apply is clicked. Closing the popover without applying discards it.

QuickSelectOption and SingleDateShortcut

PropTypeDefaultDescription
labelstring—Button text.
valuestring—A unique key.
getRange / getDate() => { start: Date; end: Date } | () => Date—Called when the panel renders and on click, so relative dates stay current. QuickSelectOption has getRange, SingleDateShortcut has getDate.