# Plain Arithmetic

Industry: Core

Plain arithmetic operates on timezone-free values. Use it when you want calendar
math that ignores timezones entirely — "add 30 days", "what's the difference in
hours", "start of this month".

## Add and subtract

```typescript
import {
  addDate,
  addBusinessDays,
  subtractBusinessDays,
  subtractDateTime,
} from "@northguild/gmt";

addDate("2026-01-01", { days: 90 });
// "2026-04-01"

addBusinessDays("2024-03-15", 1);
// "2024-03-18" — skips the weekend

subtractBusinessDays("2024-03-18", 1);
// "2024-03-15" — skips the weekend going backward

subtractDateTime("2024-03-15T14:30:45", { hours: 2 });
// "2024-03-15T12:30:45"
```

With one argument, `addBusinessDays`/`subtractBusinessDays` use a fixed ISO
Monday–Friday week, with no locale parameter and no holidays. They match the boundary
that `isBusinessDay` checks. Pass a `BusinessCalendar` as the last argument to state
the weekend and the holidays yourself:

```typescript
import { addBusinessDays, subtractBusinessDays } from "@northguild/gmt";

const london = {
  weekend: [6, 7],
  holidays: ["2024-05-31", "2024-12-25"],
  timeZone: "Europe/London",
};

addBusinessDays("2024-12-24", 1);
// "2024-12-25" — no calendar, so the Wednesday counts

addBusinessDays("2024-12-24", 1, london);
// "2024-12-26" — 25 December is a holiday, so the count skips it

subtractBusinessDays("2024-12-26", 1, london);
// "2024-12-24"
```

The rest of the business-day family (counting days between two dates, rolling a date
onto a working day, merging calendars) is in
[Business Calendars and Roll Conventions](../business-calendars/).

A year-month such as a card expiry has `addYearMonth` and `subtractYearMonth`, which take only years and months: `addYearMonth("2026-12", { months: 1 })` gives `"2027-01"`.

## Control out-of-range results with `overflow`

`add*`/`subtract*` accept an optional `overflow` (`"constrain"` (default) | `"reject"`).
`addTime`/`subtractTime` are the exception: they take no options argument, because a
clock time wraps around midnight, as Temporal's `PlainTime#add` does. `addDate` shows both modes:

```typescript
import { addDate } from "@northguild/gmt";

addDate("2024-01-31", { months: 1 });
// "2024-02-29" — constrain clamps to the last valid day

addDate("2024-01-31", { months: 1 }, { overflow: "reject" });
// "" — Feb 31 doesn't exist and "reject" refuses to clamp it

addDate("2024-02-29", { years: 1 });
// "2025-02-28" — "constrain" (the default) clamps to the last valid day, as Temporal does
```

## Diff (difference between two values)

```typescript
import { diffDate, diffDateTime } from "@northguild/gmt";

diffDateTime("2024-03-17T12:00:00", "2024-03-17T12:30:00", "minutes");
// 30

diffDate("2024-03-15", "2024-03-20", "days");
// 5
```

Need a full multi-unit breakdown (e.g. `"P26D"`) instead of a single-unit number?
Use the durations bridge functions like `diffDateAsDuration`.

For year-months, `diffYearMonth("2025-11", "2026-10", "months")` gives `11`.

## Round a diff result

`diff*` accept optional `smallestUnit`/`roundingIncrement`/`roundingMode`:

```typescript
import { diffDate } from "@northguild/gmt";

diffDate("2023-01-01", "2023-01-10", "weeks", {
  smallestUnit: "week",
  roundingMode: "halfExpand",
});
// 1
```

## Start and end of a unit

```typescript
import { startOfDate, endOfDate, startOfDateTime, endOfDateTime } from "@northguild/gmt";

startOfDateTime("2024-02-29T12:34:56", "day"); // "2024-02-29T00:00:00" — start of day
endOfDateTime("2024-02-29T12:34:56", "day"); // "2024-02-29T23:59:59.999999999" — end of day

startOfDate("2024-03-15", "month"); // "2024-03-01"
endOfDate("2024-03-15", "month"); // "2024-03-31"
```

A year-month has `startOfYearMonth` and `endOfYearMonth`, and `getDaysInYearMonth`: `endOfYearMonth("2024-02")` gives `"2024-02-29"` and `getDaysInYearMonth("2024-02")` gives `29`.

## Quarter boundaries

```typescript
import { startOfQuarterForDate, endOfQuarterForDate } from "@northguild/gmt";

startOfQuarterForDate("2024-03-15"); // "2024-01-01"
endOfQuarterForDate("2024-03-15"); // "2024-03-31"
```

## Clamp a date to a range

```typescript
import { clampDate } from "@northguild/gmt";

clampDate("2024-02-01", "2024-03-01", "2024-03-31");
// "2024-03-01" — below min

clampDate("2024-05-01", "2024-03-01", "2024-03-31");
// "2024-03-31" — above max
```

## Find the nearest date to a target

```typescript
import { closestDateTo } from "@northguild/gmt";

closestDateTo("2024-03-15", ["2024-03-01", "2024-03-20", "2024-03-18"]);
// "2024-03-18" — nearest by calendar distance
```

## Set one or more fields directly

`setDate`/`setDateTime`/`setTime`/`setZoned`/`setUnix`/`setUtc` set fields atomically via `.with()` — the safe
alternative to composing `add*` calls field-by-field, which resolves each field's
overflow independently and can silently diverge on multi-field updates:

```typescript
import { setDate, setDateTime, setTime } from "@northguild/gmt";

setDate("2024-01-31", { month: 2 });
// "2024-02-29" — constrain clamps to the last valid day

setDate("2024-03-10", { year: 2025 }); // "2025-03-10"
setDateTime("2024-03-10T12:00:00", { hour: 9 }); // "2024-03-10T09:00:00"
setTime("12:00:00", { hour: 25 }); // "23:00:00" — constrain clamps
```

`setZoned` and `setUnix` also accept `disambiguation` and `offset` for DST gap and overlap
control. `setUtc` takes only `overflow`, because a UTC wall clock is never ambiguous. See
[Zoned Operations](/gmt/guides/zoned-date-operations/zoned-operations/) and
[DST Disambiguation](/gmt/guides/concepts/dst-disambiguation/).

## Cycle a single field (wrap, don't carry)

`cycleDate`/`cycleDateTime`/`cycleTime`/`cycleZoned` adjust one field and **wrap** at that
field's own min/max instead of carrying into the next larger field — the
datepicker-segment-editing primitive `add*` can't express:

```typescript
import { addDate, cycleDate } from "@northguild/gmt";

cycleDate("2024-12-15", "month", 1);
// "2024-01-15" — stays in the same year

addDate("2024-12-15", { months: 1 });
// "2025-01-15" — addDate correctly overflows into the next year
```

`options.round` on any of the four steps to the next multiple of `amount`, rather than
moving by exactly `amount`. It matches `@internationalized/date`'s `CycleOptions.round`:

```typescript
import { cycleDateTime } from "@northguild/gmt";

cycleDateTime("2024-03-15T10:07:00", "minute", 10);
// "2024-03-15T10:17:00" — exactly ten minutes on

cycleDateTime("2024-03-15T10:07:00", "minute", 10, { round: true });
// "2024-03-15T10:10:00" — the next multiple of ten
```

`cycleZoned` also accepts `disambiguation` and `offset` (default `offset: "prefer"`) for
the same DST control as `setZoned`.

## Locale-aware week boundaries

```typescript
import { getLocaleStartOfWeek, getLocaleEndOfWeek } from "@northguild/gmt";

getLocaleStartOfWeek("2024-02-29", "en-US"); // "2024-02-25" (Sunday)
getLocaleStartOfWeek("2024-02-29", "fr-FR"); // "2024-02-26" (Monday)
getLocaleEndOfWeek("2024-02-29", "en-US"); // "2024-03-02" (Saturday)
```

Unlike `startOfDate(value, "week", { weekStartsOn })`, these derive the week's
first day from the locale via `Intl.Locale.prototype.weekInfo`. The zoned equivalents are
`getLocaleZonedStartOfWeek` and `getLocaleZonedEndOfWeek`.

## See also

- [Durations](../durations/) — ISO 8601 duration strings, `durationFromUnits` to write a unit object as one, and the `*AsDuration` bridge
- [Comparisons](../comparisons/) — ordering, equality, weekend/business-day checks
- [Business Calendars and Roll Conventions](../business-calendars/) — holidays, weekends and rolling onto a working day
- [Zoned Operations](/gmt/guides/zoned-date-operations/zoned-operations/) — timezone-aware arithmetic
- [`addDate` reference](/gmt/reference/plain/calculate/addDate/)
- [`diffDate` reference](/gmt/reference/plain/calculate/diffDate/)
- [`startOfDate` reference](/gmt/reference/plain/calculate/startOfDate/)
