# Date Arithmetic

Industry: Core

GMT's arithmetic functions handle month overflow, DST transitions, and calendar
boundaries correctly. Manual arithmetic almost always misses at least one edge case.

## Mistakes

### Using manual date arithmetic

Severity: high.

Adding days by concatenating strings or manipulating the date parts manually ignores month lengths, leap years, and DST. GMT's add/subtract functions use Temporal arithmetic under the hood.

Wrong:
```js
const next = dateStr.split("-");
next[2] = String(Number(next[2]) + 1); // "2024-02-31" → "2024-02-32" — invalid
```

Right:
```js
import { addDate } from "@northguild/gmt";

addDate("2024-02-29", { days: 1 }); // "2024-03-01" — correct
```

### Not handling month overflow

Severity: high.

Adding N days to a date near month-end can cross into the next month. Manual arithmetic that assumes 30 or 31 days per month breaks at every boundary.

Wrong:
```js
const [y, m, d] = "2024-01-31".split("-").map(Number);
const newD = d + 1; // 32 — invalid day
```

Right:
```js
import { addDate } from "@northguild/gmt";

addDate("2024-01-31", { months: 1 }); // "2024-02-29" — leap-year-aware
```

### Not handling invalid input

Severity: medium.

Arithmetic functions return an empty string for invalid input. Passing a malformed date silently produces an empty string rather than throwing — check the result if the input comes from an external source.

Wrong:
```js
const result = addDate(userInput, { days: 5 });
// if userInput is "not-a-date", result is "" — easy to miss
```

Right:
```js
import { addDate } from "@northguild/gmt";

const result = addDate(userInput, { days: 5 });
if (result === "") {
  // input was invalid
}
```

### Not handling leap year

Severity: medium.

February 29 exists only in leap years. Adding or subtracting years across a leap-day boundary without a calendar-aware function produces February 28 or an invalid date.

Wrong:
```js
const [y, m, d] = "2024-02-29".split("-");
const nextYear = [Number(y) + 1, m, d].join("-"); // "2025-02-29" — invalid
```

Right:
```js
import { addDate } from "@northguild/gmt";

addDate("2024-02-29", { years: 1 }); // "2025-02-28" — leap-year-aware
```

### Reaching for cycleDate when calendar arithmetic is wanted (or vice versa)

Severity: high.

cycleDate moves one field around its own range and wraps without carrying into the next field — it is not calendar arithmetic. addDate / subtractDate / diffDate handle month/year boundaries correctly; cycleDate does not.

Wrong:
```js
cycleDate("2024-12-15", "month", 1); // "2024-01-15" — wraps within the year, not calendar arithmetic
```

Right:
```js
import { addDate } from "@northguild/gmt";

addDate("2024-12-15", { months: 1 }); // "2025-01-15" — calendar arithmetic
```

## See also

- [Core Date Operations guide](/gmt/guides/core-date-operations/plain-arithmetic/)
- [`addDate` reference](/gmt/reference/plain/calculate/addDate/)
- [`subtractDate` reference](/gmt/reference/plain/calculate/subtractDate/)
- [`cycleDate` reference](/gmt/reference/plain/calculate/cycleDate/)
- [`diffDate` reference](/gmt/reference/plain/calculate/diffDate/)
