Skip to content

Date adapters

The calendar never formats, computes or even reads dates itself: an adapter does. Its date type is the one the calendar takes and gives: Date with the date-fns, Day.js and Luxon adapters, Temporal.ZonedDateTime with the Temporal one.

Adapter Date type Time zone Peer dependency
@weekwise/adapter-date-fns Date the browser’s date-fns
@weekwise/adapter-dayjs Date the browser’s dayjs
@weekwise/adapter-luxon Date any IANA zone luxon 3.4+
@weekwise/adapter-temporal Temporal.ZonedDateTime any IANA zone none
import { DateFnsAdapter } from "@weekwise/adapter-date-fns";
import { fr } from "date-fns/locale/fr";
const adapter = new DateFnsAdapter({ locale: fr });

Give the adapter to the UI package: <DateLocalizationProvider adapter={adapter}> in React, createCalendar(element, { adapter }) in the DOM package.

Every adapter has built-in patterns you can override in its own tokens:

new DateFnsAdapter({ locale: zhCN, formats: { timeShort: "HH:mm", weekdayShort: "EEEEEE" } });
Pattern Used for
timeShort Hour labels, now line, drag preview
date Drag preview
weekdayShort Column headers, date picker
dateTime, dateTimeShort, dateTimeLong, … Your own format(date, "dateTime") calls

Temporal has no format strings, so its patterns are either Intl.DateTimeFormat options or tokens (dd.MM.yyyy).

Any object implementing DateAdapter<TDate> from @weekwise/core works. The calendar takes and gives dates of that type and never looks inside them.

import type { DateAdapter } from "@weekwise/core";
export class MyAdapter implements DateAdapter<MyDate> {
readonly locale = "en-GB"; // BCP 47 code: interface language and heading formats
readonly weekStartsOn = 1; // default first day of the week
readonly timeZone = "Europe/Berlin"; // optional IANA zone; omit for the runtime's zone
toMillis(date: MyDate | number | string) { /* epoch ms: keys, sorting, timers */ }
fromMillis(millis: number): MyDate { /* the date at epoch ms */ }
// implement every other method of the interface
}

Methods take TDate | number | string (a timestamp or an ISO string) and return TDate. Wall-clock methods (getHours, getStartOfDay, addDays…) work in the adapter’s time zone. @weekwise/adapter-date-fns is a reference implementation for Date, @weekwise/adapter-temporal for another date type and time zones.