Build a month grid
Lay out the weeks of a month with monthGrid, weekRange and iteratePlainDates, for a date picker or a view of your own.
A month grid needs three answers: which date the first row starts on, how many rows there are,
and which days belong to the months before and after. monthGrid gives all three for any date and
any first day of the week. This page builds the grid as data. It imports no UI framework, so the
same rows feed a date picker, a view or a printed report.
The rows
monthGrid(date, { weekStartsOn }) returns the month's first day as month, the whole visible
range as range, and one seven-day range per row as weeks. iteratePlainDates steps through a
range one date at a time and leaves the range's end out:
import {
iteratePlainDates,
monthGrid,
weekRange,
type WeekStartsOn,
} from "@moltenship/calendar-temporal";
export interface GridDay {
readonly date: Temporal.PlainDate;
/** False for the days of the months before and after, which fill the first and last rows. */
readonly inMonth: boolean;
}
/** The rows of a month grid: one array of seven days for each week. */
export function monthRows(date: Temporal.PlainDate, weekStartsOn: WeekStartsOn): GridDay[][] {
const grid = monthGrid(date, { weekStartsOn });
return grid.weeks.map((week) =>
Array.from(iteratePlainDates(week), (day) => ({
date: day,
inMonth: day.year === grid.month.year && day.month === grid.month.month,
})),
);
}weekStartsOn is an ISO day number: 1 is Monday and 7 is Sunday. The function takes it as an
argument and reads no locale, so pass the first day your app uses.
A grid has four to six rows. February 2027 starts on a Monday and has 28 days, so with weeks that start on Monday it has four. August 2026 has six.
The weekday names
The names above the columns come from any week that starts on the same day. weekRange returns
the seven-day range around a date:
/** The names above the columns, starting on the grid's first weekday. */
export function weekdayNames(weekStartsOn: WeekStartsOn, locale: string): string[] {
// Any week has the seven weekdays in the grid's order.
const week = weekRange(Temporal.PlainDate.from("2026-01-01"), { weekStartsOn });
return Array.from(iteratePlainDates(week), (day) =>
day.toLocaleString(locale, { weekday: "short" }),
);
}Draw it
A component maps over the rows. This function writes them as text, which shows the layout without a framework:
/** The grid as text, with the days of other months left blank. */
export function monthText(date: Temporal.PlainDate, weekStartsOn: WeekStartsOn): string {
const cell = (text: string) => text.slice(0, 2).padStart(2);
const rows = monthRows(date, weekStartsOn).map((row) =>
row.map((day) => cell(day.inMonth ? `${day.date.day}` : "")).join(" "),
);
return [weekdayNames(weekStartsOn, "en-US").map(cell).join(" "), ...rows].join("\n");
}monthText(Temporal.PlainDate.from("2026-10-08"), 1) returns:
Mo Tu We Th Fr Sa Su
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31The first row starts on September 28. Those three days are in the grid with inMonth false, so a
component can draw them dimmed, and this function leaves them blank.
Six rows every month
A grid that changes height as the reader moves through the year makes the page jump. Pass
fixedWeeks: true for six rows in every month:
/** Six rows for every month, so a grid keeps its height as the reader moves through the year. */
export function sixWeeks(date: Temporal.PlainDate, weekStartsOn: WeekStartsOn) {
return monthGrid(date, { weekStartsOn, fixedWeeks: true });
}For February 2027 the fixed grid runs to March 15, two rows into March.
Find the events in the grid
grid.range is the range of dates to load events for. It includes the days of the months around
the month, since the grid shows them. For events stored as instants, turn the range's dates into
instants in the viewer's time zone with zonedDayRange, as
Handle daylight saving time shows.
A calendar view gets more than this from projectMonth in @moltenship/calendar-core, which lays
out events on these rows with their lanes.
In your own views draws a month view from it.