# Dates

> Dependency-free helpers to compute, compare and format dates, in local time or in any time zone, the same ones behind the calendar and pickers.

Source: https://eunomia-ds.com/utilities/dates/

`@eunomia/core/date.js` gathers the date helpers the Eunomia calendar, scheduler and pickers are built on. They work on plain `Date` objects, have no dependency, and cover the needs of an interface: the start and end of a period, adding days or months, comparing two dates, laying out a month, and reading or setting a time in a given time zone.

```js
import { startOfWeek, addMonths, isSameDay } from "@eunomia/core/date.js";
```

The module registers no component, so it's safe to import anywhere, including on the server. It comes with `@eunomia/elements` and `@eunomia/charts`; under pnpm or Yarn's strict modes, add `@eunomia/core` to your own dependencies to import it.

Every function returns a new value and never changes the date you pass in, unlike `Date`'s own `setDate` or `setMonth`.

> **Two families**
>
> The **local time** functions read and write a date the way `Date` does, in the visitor's own time zone: they take no time zone. The **time zone** functions take an IANA name (`"Europe/Paris"`) explicitly, to show or edit a time as it reads in that place, whatever the visitor's own zone.

## Try it

Pick a family, then a function: its inputs show up next to it, and the call and its result update as you change them. The result is the one the function returns, run in your browser, in your own time zone.

en-US en-GB fr-FR de-DE ja-JP

```

```

## Start and end of a period

| Function | Returns |
| --- | --- |
| `startOfHour(date)` | The start of its hour, `HH:00:00.000` |
| `startOfDay(date)` | The start of its day, `00:00:00.000` |
| `endOfDay(date)` | The end of its day, `23:59:59.999` |
| `startOfWeek(date, weekStartsOn?)` | The first day of its week, at the start of the day |
| `endOfWeek(date, weekStartsOn?)` | The last day of its week, at the end of the day |
| `startOfMonth(date)` | The 1st of its month, at the start of the day |
| `endOfMonth(date)` | The last day of its month, at the end of the day |
| `startOfYear(date)` | January 1st of its year, at the start of the day |
| `endOfYear(date)` | December 31st of its year, at the end of the day |

Weeks start on Monday. Pass `0` as `weekStartsOn` for weeks starting on Sunday (`1`, Monday, is the default):

```js
import { startOfWeek, endOfMonth } from "@eunomia/core/date.js";

const date = new Date(2026, 10, 5); // Thursday, November 5, 2026

startOfWeek(date); // Monday, November 2, 00:00
startOfWeek(date, 0); // Sunday, November 1, 00:00
endOfMonth(date); // Monday, November 30, 23:59:59.999
```

## Adding and subtracting

`addHours`, `addDays`, `addWeeks`, `addMonths` and `addYears` take a date and an amount, negative to go back in time.

```js
import { addDays, addMonths } from "@eunomia/core/date.js";

addDays(new Date(2026, 10, 30), 1); // December 1, 2026
addMonths(new Date(2026, 0, 31), 1); // February 28, 2026
addMonths(new Date(2026, 2, 15), -3); // December 15, 2025
```

They move along the calendar, not by a number of milliseconds:

- a day stays a day across a daylight saving change, where adding 24 hours would land an hour off;
- a month or a year is clamped to the last day of a shorter month: January 31 plus one month is February 28 (29 in a leap year), not March 3 as `setMonth` gives.

## Comparing

| Function | Is `true` when |
| --- | --- |
| `isSameDay(a, b)` | Both dates fall on the same calendar day, whatever the time |
| `isSameMonth(a, b)` | Both fall in the same month of the same year |
| `isSameYear(a, b)` | Both fall in the same year |
| `isToday(date)` | The date falls today |
| `isWeekend(date)` | The date falls on a Saturday or a Sunday |
| `isWithinRange(date, start, end)` | The date is between `start` and `end`, both included |

```js
import { isWithinRange, startOfMonth, endOfMonth } from "@eunomia/core/date.js";

const today = new Date();
const inThisMonth = isWithinRange(
  order.date,
  startOfMonth(today),
  endOfMonth(today),
);
```

## A month's calendar grid

`getCalendarMonthMatrix(date, weekStartsOn?)` lays out the month of `date` as 6 weeks of 7 days, with the days of the months before and after that complete its first and last weeks: the grid of a month view. It always has 6 rows, so the grid keeps the same height from one month to the next.

```js
import { getCalendarMonthMatrix, isSameMonth } from "@eunomia/core/date.js";

const month = new Date(2026, 10, 1);
const weeks = getCalendarMonthMatrix(month);

weeks[0][0]; // Monday, October 26, 2026: the grid starts in October
weeks.flat().filter((day) => isSameMonth(day, month)).length; // 30
```

## Writing a range

`formatDateRange(start, end, locale?)` writes a period without repeating what both ends share, in the given locale (the visitor's own by default):

```js
import { formatDateRange } from "@eunomia/core/date.js";

formatDateRange(new Date(2026, 6, 12), new Date(2026, 6, 18), "en-GB");
// "12 – 18 July 2026"
formatDateRange(new Date(2026, 5, 28), new Date(2026, 6, 4), "en-GB");
// "28 Jun – 4 Jul 2026"
formatDateRange(new Date(2026, 11, 28), new Date(2027, 0, 3), "en-GB");
// "28 Dec 2026 – 3 Jan 2027"
```

## Time zones

A `Date` is an instant, the same everywhere. What changes from one place to another is how it reads: 9:00 in Paris is 8:00 in London. These functions read and write the time an instant shows in a given IANA time zone, whatever the visitor's own, for example to show a meeting at the time of the office that organizes it.

| Function | What it does |
| --- | --- |
| `getRuntimeTimeZone()` | The visitor's own time zone, such as `"Europe/Paris"` |
| `getZonedParts(date, timeZone?)` | The year, month (1 to 12), day, hour, minute, second and weekday (0 for Sunday) the instant shows there |
| `zonedTimeToUtc(wallTime, timeZone)` | The instant at which a time zone's clock shows the given date and time |
| `setZonedTime(date, timeZone, hour, minute)` | The same day, as read in that time zone, at another time |
| `getTimeZoneOffsetMinutes(date, timeZone)` | The time zone's offset from UTC at that instant, in minutes: `60` for Paris in winter |
| `getTimeZoneOffsetLabel(date, timeZone, options?)` | A short label of that offset: `"GMT-5"` for New York, or `"EST"` with `{ style: "short" }` where the locale has an abbreviation |
| `isSameTimeZone(a?, b?)` | Whether two time zones have the same name, a missing one meaning the visitor's own |

```js
import {
  getZonedParts,
  zonedTimeToUtc,
  getTimeZoneOffsetLabel,
} from "@eunomia/core/date.js";

// A meeting at 9:00 in Paris on December 1, 2026
const start = zonedTimeToUtc(
  { year: 2026, month: 12, day: 1, hour: 9, minute: 0 },
  "Europe/Paris",
);

start.toISOString(); // "2026-12-01T08:00:00.000Z"
getZonedParts(start, "America/New_York").hour; // 3
getTimeZoneOffsetLabel(start, "Europe/Paris"); // "GMT+1"
```

The offset of a time zone changes with daylight saving time, which is why each function takes a date: Paris is at `+60` in winter and `+120` in summer. Around a change of time, a time that doesn't exist (2:30 when clocks go from 2:00 to 3:00) resolves with the offset in effect after the change, and a time that happens twice (when clocks go back) always resolves to the same one of the two.

The names are compared as written: two names of the same zone, such as `Asia/Calcutta` and `Asia/Kolkata`, aren't recognized as one by `isSameTimeZone`.

## Out of scope

These helpers cover what an interface needs, not every use of dates. They don't parse date strings in any format, handle calendars other than the Gregorian one, or write durations and relative times ("in 3 days") beyond what `Intl` provides. For those, use a dedicated date library, or `Intl.RelativeTimeFormat`.
