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—'use client';
import { format } from 'date-fns';
import { useState } from 'react';
import { DateRangePicker } from '@/components/dentil/date-picker/date-picker';
export function DatePickerOverviewDemo() {
const [period, setPeriod] = useState<{ start: Date | undefined; end: Date | undefined }>();
return (
<div className="flex flex-col items-start gap-4">
<DateRangePicker value={period} onChange={setPeriod} showQuickSelect className="w-72" />
<span className="text-muted-foreground text-sm">
Result:{' '}
<code className="text-foreground">
{period?.start && period.end
? `${format(period.start, 'yyyy-MM-dd HH:mm')} → ${format(period.end, 'yyyy-MM-dd HH:mm')}`
: '—'}
</code>
</span>
</div>
);
}Installation
pnpm dlx shadcn@latest add @dentil/date-pickernpx shadcn@latest add @dentil/date-pickeryarn dlx shadcn@latest add @dentil/date-pickerbunx --bun shadcn@latest add @dentil/date-pickerFiles 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.
—'use client';
import { addDays, format } from 'date-fns';
import { useState } from 'react';
import { SingleDatePicker } from '@/components/dentil/date-picker/date-picker';
export function DatePickerSingleDemo() {
const [dueDate, setDueDate] = useState<Date>();
const today = new Date();
return (
<div className="flex flex-col items-start gap-4">
<SingleDatePicker
value={dueDate}
onChange={setDueDate}
placeholder="Due date"
minDate={today}
maxDate={addDays(today, 90)}
showShortcuts
className="w-60"
/>
<span className="text-muted-foreground text-sm">
Result: <code className="text-foreground">{dueDate ? format(dueDate, 'yyyy-MM-dd') : '—'}</code>
</span>
</div>
);
}Billing cycles
Each cycle picks the last second before the next period starts. Type a number to pick any cycle count.
—'use client';
import { format, startOfMonth } from 'date-fns';
import { useState } from 'react';
import { SingleDatePicker } from '@/components/dentil/date-picker/date-picker';
const subscriptionStart = startOfMonth(new Date());
export function DatePickerCyclesDemo() {
const [cancelAt, setCancelAt] = useState<Date>();
return (
<div className="flex flex-col items-start gap-4">
<SingleDatePicker
value={cancelAt}
onChange={setCancelAt}
placeholder="Cancel subscription on"
minDate={subscriptionStart}
showShortcuts
shortcuts={[]}
cycleConfig={{ startDate: subscriptionStart, durationMonths: 1 }}
className="w-64"
/>
<span className="text-muted-foreground text-sm">
Result: <code className="text-foreground">{cancelAt ? format(cancelAt, 'yyyy-MM-dd HH:mm:ss') : '—'}</code>
</span>
</div>
);
}Apply step
Pick a range, then close the popover without applying: onChange isn't called and the selection resets.
0'use client';
import { format } from 'date-fns';
import { useState } from 'react';
import { DateRangePicker } from '@/components/dentil/date-picker/date-picker';
export function DatePickerApplyDemo() {
const [period, setPeriod] = useState<{ start: Date | undefined; end: Date | undefined }>();
const [applied, setApplied] = useState(0);
return (
<div className="flex flex-col items-start gap-4">
<DateRangePicker
value={period}
onChange={(next) => {
setPeriod(next);
setApplied((count) => count + 1);
}}
showQuickSelect
showApplyButton
className="w-72"
/>
<span className="text-muted-foreground text-sm">
onChange calls: <code className="text-foreground">{applied}</code>
{period?.start && period.end && (
<>
{' '}
·{' '}
<code className="text-foreground">{`${format(period.start, 'd MMM')} → ${format(period.end, 'd MMM')}`}</code>
</>
)}
</span>
</div>
);
}Unix timestamps
With returnFormat="timestamp", value and onChange use unix seconds, ready for an API.
—'use client';
import { useState } from 'react';
import { SingleDatePicker } from '@/components/dentil/date-picker/date-picker';
export function DatePickerTimestampDemo() {
const [startsAt, setStartsAt] = useState<number>();
return (
<div className="flex flex-col items-start gap-4">
<SingleDatePicker returnFormat="timestamp" value={startsAt} onChange={setStartsAt} className="w-60" />
<span className="text-muted-foreground text-sm">
Result: <code className="text-foreground">{startsAt ?? '—'}</code>
</span>
</div>
);
}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.
| Prop | Type | Default | Description |
|---|---|---|---|
| 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. |
| value | Date | 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. |
| placeholder | string | 'Select a date' | 'Select a date range' | Trigger label when there is no value. |
| dateFormat | string | 'd MMM yyyy' | A date-fns format string for the trigger label and cycle dates. |
| className | string | — | Applied to the root, which wraps the trigger and clear button. Set the width here. |
| disabled | boolean | false | Disables the trigger and every day. |
| showClear | boolean | true | Show 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. |
| endOfDay | boolean | true | Move a range’s end to 23:59:59.999 of its day, so the range includes the whole last day. |
| showQuickSelect | boolean | false | Range mode: show presets beside the calendar. |
| presets | QuickSelectOption[] | quickSelectOptions | Range presets: last 7 days, 4 weeks, 6 and 12 months, and month, quarter and year to date. |
| showShortcuts | boolean | false | Single mode: show shortcuts and cycleConfig cycles beside the calendar. |
| shortcuts | SingleDateShortcut[] | defaultSingleDateShortcuts | Today 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. |
| minDate | Date | number | — | Days before it are disabled, and shortcuts and cycles before it are greyed out. |
| maxDate | Date | number | — | Days after it are disabled. |
| defaultMonth | Date | number | — | Month shown on open. Defaults to the selected date, then minDate, then today. |
| showApplyButton | boolean | false | Hold the selection until Apply is clicked. Closing the popover without applying discards it. |
QuickSelectOption and SingleDateShortcut
| Prop | Type | Default | Description |
|---|---|---|---|
| label | string | — | Button text. |
| value | string | — | 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. |