Date picker
Selects a date range from presets or a calendar, such as the period an analytics chart covers.
Presets
Presets list common ranges next to a two-month calendar.
•
•
•
•
<.date_picker
id="date-picker-presets"
name="analytics-date-range"
selected_preset="last-7-days"
on_period_change="analytics_period_changed"
presets={[
%{id: "last-24-hours", label: "Last 24 hours", period: {24, :hour}},
%{id: "last-7-days", label: "Last 7 days", period: {7, :day}},
%{id: "last-30-days", label: "Last 30 days", period: {30, :day}},
%{id: "last-12-months", label: "Last 12 months", period: {12, :month}},
%{id: "custom", label: "Custom"}
]}
>
<:actions>
<.button
label="Cancel"
variant="secondary"
phx-click={JS.dispatch("phx:date-picker-cancel", detail: %{id: "date-picker-presets"})}
/>
<.button
label="Apply"
phx-click={JS.dispatch("phx:date-picker-apply", detail: %{id: "date-picker-presets"})}
/>
</:actions>
</.date_picker>
Custom range
With the
custom
preset and a
period
, the trigger shows the selected dates.
•
•
•
•
<.date_picker
id="date-picker-custom"
name="analytics-date-range"
selected_preset="custom"
period={{~U[2026-09-01 00:00:00Z], ~U[2026-09-30 23:59:59Z]}}
presets={[
%{id: "last-7-days", label: "Last 7 days", period: {7, :day}},
%{id: "last-30-days", label: "Last 30 days", period: {30, :day}},
%{id: "custom", label: "Custom"}
]}
>
<:actions>
<.button
label="Cancel"
variant="secondary"
phx-click={JS.dispatch("phx:date-picker-cancel", detail: %{id: "date-picker-custom"})}
/>
<.button
label="Apply"
phx-click={JS.dispatch("phx:date-picker-apply", detail: %{id: "date-picker-custom"})}
/>
</:actions>
</.date_picker>
Selectable range
Days before
min
or after
max
can't be picked.
start_of_week
sets the first column.
•
•
•
•
<.date_picker
id="date-picker-limits"
name="builds-date-range"
label="Select a period"
min={Date.add(Date.utc_today(), -90)}
max={Date.utc_today()}
start_of_week={1}
presets={[
%{id: "last-7-days", label: "Last 7 days", period: {7, :day}},
%{id: "last-30-days", label: "Last 30 days", period: {30, :day}},
%{id: "custom", label: "Custom"}
]}
>
<:actions>
<.button
label="Cancel"
variant="secondary"
phx-click={JS.dispatch("phx:date-picker-cancel", detail: %{id: "date-picker-limits"})}
/>
<.button
label="Apply"
phx-click={JS.dispatch("phx:date-picker-apply", detail: %{id: "date-picker-limits"})}
/>
</:actions>
</.date_picker>
Disabled
•
•
•
•
<.date_picker
id="date-picker-disabled"
name="analytics-date-range"
selected_preset="last-30-days"
disabled
presets={[
%{id: "last-7-days", label: "Last 7 days", period: {7, :day}},
%{id: "last-30-days", label: "Last 30 days", period: {30, :day}},
%{id: "custom", label: "Custom"}
]}
/>
Guidelines
-
Give each preset a
periodof{amount, unit}, where the unit is:hour,:day,:week,:month, or:year. The range ends now and starts that long ago. -
Add a preset with the id
customfor ranges picked on the calendar. When it is selected andperiodis set, the trigger shows the dates instead of a preset label. -
Put Cancel and Apply buttons in the
:actionsslot. They dispatchphx:date-picker-cancelandphx:date-picker-applywith the picker'sid. -
Applying sends
on_period_changewith the range'sstartandendas ISO 8601 strings undervalue, and thepresetid. Store them and pass them back asperiodandselected_preset, withperiodas a tuple of twoDateTimestructs. -
Set
maxto today when there is no data for future dates, as in analytics.
API reference
<.date_picker>
| Attribute | Type | Default | Description |
|---|---|---|---|
|
disabled
|
boolean
|
false
|
Whether the date picker is disabled
|
|
id
Required
|
string
|
–
|
Unique identifier for the date picker component
|
|
label
|
string
|
"Select date range"
|
Label displayed on the trigger button
|
|
max
|
any
|
nil
|
Maximum selectable date (Date, DateTime, or ISO8601 string)
|
|
min
|
any
|
nil
|
Minimum selectable date (Date, DateTime, or ISO8601 string)
|
|
name
|
string
|
nil
|
The name attribute for the hidden input field
|
|
on_cancel
|
string
|
nil
|
Event handler name for when the cancel button is clicked
|
|
on_period_change
|
string
|
nil
|
Event handler name for when the selected date range changes
|
|
open
|
boolean
|
false
|
Whether the date picker should be open by default (useful for storybook)
|
|
period
|
any
|
nil
|
The currently selected range as a tuple {start_datetime, end_datetime}. Both values must be DateTime structs.
|
|
presets
Required
|
list
|
–
|
List of preset options. Each preset is a map with :id, :label, and optional :period keys. Period is a tuple of {amount, unit} where unit is :hour, :day, :week, :month, or :year
|
|
selected_preset
|
string
|
nil
|
The ID of the currently selected preset
|
|
start_of_week
|
integer
|
0
|
The day the week starts on (0 = Sunday, 1 = Monday, etc.)
|
Also accepts global attributes.
| Slot | Description |
|---|---|
|
:actions
|
Action buttons for the footer (e.g., Cancel and Apply). Use JS.dispatch("phx:date-picker-cancel", detail: %{id: id}) and JS.dispatch("phx:date-picker-apply", detail: %{id: id}) to trigger the date picker's cancel and apply actions.
|