# Recurring events

> Repeat events with RFC 5545 RRULE and EXDATE, edit or delete single occurrences or whole series, and use the recurrence engine on its own.

A recurring event is one event object with an `rrule`. The calendar expands it into occurrences for the visible range only, so a daily stand-up that runs for years costs nothing until you look at a given week.

## Add a recurrence rule

Set `rrule` to an RFC 5545 rule, with or without the `RRULE:` prefix. The event's `start` and `end` describe the first occurrence, and every occurrence has the same duration.

```ts
import { createCalendar } from '@birchui/calendar';

createCalendar(container, {
  view: 'month',
  date: '2026-12-01',
  timeZone: 'America/Chicago',
  events: [
    {
      id: 'standup',
      title: 'Support team stand-up',
      start: '2026-12-01T09:00',
      end: '2026-12-01T09:15',
      rrule: 'FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR',
    },
    {
      id: 'maintenance',
      title: 'Server maintenance window',
      start: '2026-12-08T22:00',
      end: '2026-12-09T02:00',
      rrule: 'FREQ=MONTHLY;BYDAY=2TU',
    },
    {
      id: 'one-on-one',
      title: '1:1 with Priya',
      start: '2026-12-01T10:00',
      end: '2026-12-01T10:30',
      rrule: 'FREQ=WEEKLY;INTERVAL=2;BYDAY=TU,TH;COUNT=6',
    },
    {
      id: 'payroll',
      title: 'Payroll cut-off',
      start: '2026-12-31',
      allDay: true,
      rrule: 'FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-1',
    },
  ],
});
```

Some rules and what they produce:

| Rule | Starting | Occurrences |
| --- | --- | --- |
| `FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR` | Tue 1 Dec 2026 | Every weekday. |
| `FREQ=MONTHLY;BYDAY=2TU` | Tue 8 Dec 2026 | The second Tuesday of each month: 12 Jan, 9 Feb, 9 Mar 2027… |
| `FREQ=WEEKLY;INTERVAL=2;BYDAY=TU,TH;COUNT=6` | Tue 1 Dec 2026 | 1, 3, 15, 17, 29 and 31 Dec, then stops. |
| `FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-1` | Thu 31 Dec 2026 | The last weekday of each month: 29 Jan, 26 Feb, 31 Mar 2027… |
| `FREQ=YEARLY;BYMONTH=11;BYDAY=4TH` | Thu 26 Nov 2026 | The fourth Thursday of November each year. |
| `FREQ=MONTHLY;BYMONTHDAY=-1` | any | The last day of each month. |

The event's own `start` is always the first occurrence and counts towards `COUNT`, as in RFC 5545, so start a series on a date the rule produces.

### Supported rule parts

| Part | Notes |
| --- | --- |
| `FREQ` | `DAILY`, `WEEKLY`, `MONTHLY` or `YEARLY`. Required. |
| `INTERVAL` | Every n-th period. Defaults to 1. |
| `COUNT` | Total number of occurrences. Cannot be combined with `UNTIL`. |
| `UNTIL` | Last possible occurrence: a date (`20270331`), a floating date-time (`20270331T090000`) or a UTC date-time (`20270331T140000Z`). |
| `BYDAY` | Weekdays `MO` to `SU`, with ordinals in monthly and yearly rules: `2TU`, `-1FR`. |
| `BYMONTHDAY` | Days of the month, including negative values counted from the end. |
| `BYMONTH` | Months 1 to 12. |
| `BYSETPOS` | Pick positions from each period's set, such as `-1` for the last. |
| `BYHOUR`, `BYMINUTE`, `BYSECOND` | Several times per day. |
| `WKST` | First day of the week for weekly rules. Defaults to `MO`. |

`FREQ=HOURLY`, `MINUTELY` and `SECONDLY` are rejected, and the rule parts `BYWEEKNO` and `BYYEARDAY` are not supported. A rule with unsupported parts still loads: the parts are kept, reported once in the console and ignored. A rule that cannot be parsed at all shows only the event's first occurrence, with a console warning naming the event.

## Exceptions with EXDATE

List the occurrences to skip in `exdates`. Each entry is matched against an occurrence's start in the event's time zone:

- a date-time, such as `'2026-12-25T09:00'`, removes the occurrence that starts at that time;
- a date only, such as `'2026-12-25'`, removes the occurrence on that day whatever its time;
- an instant with an offset or a `Date` is converted to the event's zone first.

```ts
const standup = {
  id: 'standup',
  title: 'Support team stand-up',
  start: '2026-12-01T09:00',
  end: '2026-12-01T09:15',
  rrule: 'FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR;UNTIL=20270331',
  exdates: ['2026-12-24', '2026-12-25', '2026-12-31', '2027-01-01'],
};
```

## Occurrences

Each occurrence the calendar shows is an `EventOccurrence`. It carries the master `event`, its own `start` and `end`, and identifiers for addressing it:

| Field | Example | Meaning |
| --- | --- | --- |
| `id` | `'standup'` | The master event's `id`, as a string. |
| `occurrenceId` | `'standup@2026-12-02T09:00:00'` | Unique per occurrence: the id plus the occurrence's original local start. For all-day series the date only, as in `'payroll@2027-01-29'`. |
| `recurrenceId` | `'2026-12-02T09:00:00'` | The original start of this occurrence, in the event's zone. `undefined` for single events. |
| `recurring` | `true` | Whether the occurrence comes from a rule. |

Read occurrences for any range, for example to fill a sidebar or count bookings:

```ts
const december = calendar.getOccurrences('2026-12-01', '2027-01-01');
const standups = december.filter((occurrence) => occurrence.id === 'standup').length;
```

A series yields at most 5,000 occurrences per expanded range, which keeps a mistaken `FREQ=DAILY` without an end from freezing a year view.

## Editing occurrences

When a user drags, resizes or deletes an occurrence of a series, the payload of `eventDrop`, `eventResize` or `eventRemove` has `isRecurring: true` and `scope: 'this'`. The change is applied after your listener returns, using whatever `scope` the payload has then:

| Scope | Move or resize | Delete |
| --- | --- | --- |
| `'this'` | Adds the occurrence's `recurrenceId` to the series' `exdates` and creates a separate event for the moved occurrence. | Adds the occurrence to `exdates`. |
| `'all'` | Shifts the whole series by the same amount. Moving to another weekday shifts `BYDAY` and the existing `exdates` along with it. | Removes the whole series. |

So the common "this event or all events?" question needs only a few lines:

```ts
calendar.on('eventDrop', (change) => {
  if (change.isRecurring) change.scope = window.confirm('Move every occurrence?') ? 'all' : 'this';
});

calendar.on('eventRemove', (change) => {
  if (change.isRecurring) change.scope = window.confirm('Delete the whole series?') ? 'all' : 'this';
});
```

`scope` must be set synchronously, inside the listener. To ask with your own asynchronous dialog, call `revert()` to cancel the user's change, then apply the answer with the API:

```ts
calendar.on('eventDrop', (change) => {
  if (!change.isRecurring) return;
  change.revert();
  void moveDialog.ask(change.event.title).then((scope) => {
    if (!scope) return;
    calendar.updateOccurrence(change.occurrence.occurrenceId, { start: change.start, end: change.end }, scope);
  });
});
```

### The detached occurrence

With scope `'this'` the moved occurrence becomes its own event. It is a copy of the master without `rrule` and `exdates`, with:

- `id` set to the occurrence's `occurrenceId`, such as `'standup@2026-12-16T09:00:00'`;
- `recurrenceId`, the original start it replaces;
- `recurringEventId`, the master's `id`.

```json
{
  "id": "standup@2026-12-16T09:00:00",
  "title": "Support team stand-up",
  "start": "2026-12-16T11:30",
  "end": "2026-12-16T11:45",
  "recurrenceId": "2026-12-16T09:00:00",
  "recurringEventId": "standup",
  "allDay": false
}
```

This is the same shape `parseICS` produces for `RECURRENCE-ID` overrides and `toICS` writes back, so edited series round-trip through iCalendar files. Store detached events next to their master in your database.

### Editing from code

| Method | Description |
| --- | --- |
| `updateOccurrence(occurrenceId, changes, scope = 'this')` | Change `start`, `end` and `allDay` of one occurrence or the whole series. Times are read in the calendar's `timeZone`. Returns the detached event (scope `'this'`) or the updated master. |
| `removeOccurrence(occurrenceId, scope = 'this')` | Skip one occurrence or remove the series. |
| `updateEvent(id, changes)` | Change the master directly, for example its `title`, `rrule` or `exdates`. |

```ts
calendar.updateOccurrence('maintenance@2027-02-09T22:00:00', { start: '2027-02-10T22:00', end: '2027-02-11T02:00' });
calendar.removeOccurrence('standup@2026-12-24T09:00:00');
calendar.updateEvent('standup', { rrule: 'FREQ=WEEKLY;BYDAY=MO,WE,FR' });
```

Neither `updateOccurrence` nor `removeOccurrence` emits an event; they are your own changes.

## Time zones and recurrence

A series is expanded in its event's `timeZone` (or the calendar's zone when it has none), so occurrences keep their local time across daylight saving changes. A New York stand-up at 09:00 stays at 09:00 in New York; a London viewer sees it at 14:00 for most of the year and at 13:00 during the few weeks each spring and autumn when only one of the two countries has changed its clocks. See [Time zones](https://docs.birchui.com/calendar/time-zones.md#recurring-events-across-zones).

## Use the engine on its own

The rule engine is exported for servers, reminders and tests. It works on wall-clock times encoded as milliseconds: pass `Date.UTC(year, monthIndex, day, hour, minute)` for a local date-time and read results back with `new Date(value).toISOString()`.

```ts
import { expandRRule, formatRRule, parseRRule, RRuleError } from '@birchui/calendar';

const rule = parseRRule('FREQ=MONTHLY;BYDAY=2TU');
const firstWindow = Date.UTC(2026, 11, 8, 22, 0);

const windows = expandRRule(rule, firstWindow, {
  start: Date.UTC(2027, 0, 1),
  end: Date.UTC(2027, 6, 1),
}).map((wall) => new Date(wall).toISOString().slice(0, 16));
```

`windows` is `['2027-01-12T22:00', '2027-02-09T22:00', '2027-03-09T22:00', '2027-04-13T22:00', '2027-05-11T22:00', '2027-06-08T22:00']`.

| Function | Description |
| --- | --- |
| `parseRRule(text)` | Parses a rule into an `RRule` object. Throws `RRuleError` for invalid input, such as a missing `FREQ`, an unsupported frequency, a bad `BYDAY` value or `COUNT` together with `UNTIL`. |
| `formatRRule(rule)` | Turns an `RRule` back into text, without the `RRULE:` prefix, omitting default values. |
| `expandRRule(rule, dtstart, options?)` | Returns occurrence start times from `dtstart` on. `options.start` (inclusive) and `options.end` (exclusive) limit the window, and `options.limit` caps the result (default 5,000). |

The `RRule` object has `freq`, `interval`, `wkst` and optional `count`, `until`, `byDay`, `byMonthDay`, `byMonth`, `bySetPos`, `byHour`, `byMinute` and `bySecond`. Weekdays are numbers from `0` (Sunday) to `6` (Saturday), and `byDay` entries are `{ weekday, ordinal? }`. Building a rule in a form is therefore a matter of editing the object and calling `formatRRule`:

```ts
function describeRule(text: string): string {
  try {
    const rule = parseRRule(text);
    rule.interval = 2;
    return formatRRule(rule);
  } catch (error) {
    if (error instanceof RRuleError) return `Invalid rule: ${error.message}`;
    throw error;
  }
}

describeRule('RRULE:FREQ=WEEKLY;BYDAY=TU,TH;COUNT=6');
```

The call returns `'FREQ=WEEKLY;INTERVAL=2;COUNT=6;BYDAY=TU,TH'`.

## Next steps

- [iCalendar](https://docs.birchui.com/calendar/ical.md) imports and exports series with their exceptions.
- [Interactions](https://docs.birchui.com/calendar/interactions.md) covers the change payloads in detail.
