Date math utilities for calendar grid generation.
Handles month grid computation, week boundaries, day ranges, and date arithmetic needed by the calendar view components.
All functions are defensively coded — invalid inputs return safe defaults rather than crashing.
Week start day
All functions accept a week_start parameter (1-7, ISO day numbering):
- 1 = Monday (default, ISO standard)
- 6 = Saturday
- 7 = Sunday
This matches PhoenixKit's week_start_day setting.
Summary
Functions
Groups a flat list of dates into weeks (lists of 7 dates).
Groups events by date for efficient lookup.
Returns whether the given date is in the specified month.
Returns a flat list of dates for a month grid view.
Returns a list of dates for an N-day view starting from the given date.
Shifts the anchor date by one step for the given view.
Returns whether the given date is today.
Returns the visible date range for a given view and anchor date.
Returns a list of dates for a week containing the given date.
Returns the last day of the week containing the given date.
Returns the ISO week number for the given date.
Returns the first day of the week containing the given date.
Returns whether the given date is a weekend (Saturday or Sunday).
Returns a list of 12 {year, month} tuples for the year view.
Functions
Groups a flat list of dates into weeks (lists of 7 dates).
@spec group_events_by_date([PhoenixLiveCalendar.Event.t()], [Date.t()]) :: %{ required(Date.t()) => [PhoenixLiveCalendar.Event.t()] }
Groups events by date for efficient lookup.
Returns a map of %{Date.t() => [Event.t()]}.
Returns whether the given date is in the specified month.
Returns a flat list of dates for a month grid view.
The grid always contains complete weeks (rows of 7 days). It starts from the first day of the week containing the month's first day, and extends to fill either 5 or 6 complete weeks.
Options
week_start— First day of week (1-7, default: 1 = Monday)fixed_weeks— Always show 6 weeks (default: true). When false, shows 5 weeks if the month fits.
Examples
iex> dates = DateHelpers.month_grid(~D[2026-04-01])
iex> length(dates)
42
iex> hd(dates)
~D[2026-03-30]
@spec n_day_dates(Date.t(), pos_integer()) :: [Date.t()]
Returns a list of dates for an N-day view starting from the given date.
Examples
iex> DateHelpers.n_day_dates(~D[2026-04-01], 4)
[~D[2026-04-01], ~D[2026-04-02], ~D[2026-04-03], ~D[2026-04-04]]
Shifts the anchor date by one step for the given view.
Examples
iex> DateHelpers.shift(~D[2026-04-15], :month, :next)
~D[2026-05-15]
iex> DateHelpers.shift(~D[2026-04-15], :week, :prev)
~D[2026-04-08]
Returns whether the given date is today.
Returns the visible date range for a given view and anchor date.
Returns {start_date, end_date} (inclusive start, exclusive end).
Returns a list of dates for a week containing the given date.
Examples
iex> DateHelpers.week_dates(~D[2026-04-01])
[~D[2026-03-30], ~D[2026-03-31], ~D[2026-04-01], ~D[2026-04-02],
~D[2026-04-03], ~D[2026-04-04], ~D[2026-04-05]]
Returns the last day of the week containing the given date.
Returns the ISO week number for the given date.
Returns the first day of the week containing the given date.
Returns whether the given date is a weekend (Saturday or Sunday).
Returns a list of 12 {year, month} tuples for the year view.
Examples
iex> DateHelpers.year_months(2026)
[{2026, 1}, {2026, 2}, ..., {2026, 12}]