iCalendar import and exportCalendar
Read .ics files from Outlook, Google Calendar and Apple Calendar with parseICS, and export events, series and exceptions with toICS.
parseICS turns iCalendar text (RFC 5545, the .ics format used by Outlook, Google Calendar and Apple Calendar) into calendar events, and toICS turns events back into a file. Both are plain functions: they work in the browser, in a worker and on a server, with or without a calendar on the page.
import { parseICS, toICS } from '@birchui/calendar';Import a file#
Let users pick an .ics file and load it:
<label>
Import calendar
<input type="file" id="ics-file" accept=".ics,text/calendar" />
</label>import { createCalendar, parseICS } from '@birchui/calendar';
const calendar = createCalendar(document.getElementById('team-calendar')!, { view: 'month' });
const input = document.querySelector<HTMLInputElement>('#ics-file')!;
input.addEventListener('change', async () => {
const file = input.files?.[0];
if (!file) return;
const imported = parseICS(await file.text());
calendar.setEvents([...calendar.getEvents(), ...imported]);
statusLine.textContent = `Imported ${imported.length} events from ${file.name}`;
});Each VEVENT becomes an event. Use setEvents(imported) instead to replace what is shown, or send the events to your server first and let the calendar reload them.
What is read#
| iCalendar | Event field |
|---|---|
UID |
id (an event without one gets 'ics-event-1', 'ics-event-2' and so on) |
SUMMARY |
title |
DTSTART, DTEND or DURATION |
start, end; VALUE=DATE gives an all-day event |
TZID parameter |
timeZone |
RRULE |
rrule, without the RRULE: prefix |
EXDATE (one or more lines, comma lists) |
exdates |
RECURRENCE-ID |
a detached occurrence: its id is '<UID>@<recurrence id>', with recurrenceId and recurringEventId, and the master gets a matching exdates entry |
STATUS:CANCELLED on an override |
removes that occurrence from the series |
DESCRIPTION, LOCATION, URL, STATUS |
description, location, url, status |
CATEGORIES |
categories (an array) |
COLOR |
color |
Folded lines are unfolded and escaped text (\,, \;, \n) is decoded. A VEVENT without a DTSTART is skipped, and an event without DTEND or DURATION lasts one day (all-day) or has no duration (timed).
Dates and zones on import#
DTSTART;TZID=Europe/Berlin:20261209T160000becomesstart: '2026-12-09T16:00:00'withtimeZone: 'Europe/Berlin'.- UTC times (
…Z) become UTC strings, such as'2026-12-08T19:00:00Z'. A recurring series in UTC getstimeZone: 'UTC'so it repeats in UTC. - Times without
ZorTZIDstay floating. - Windows zone names that Outlook writes, such as
Pacific Standard TimeorW. Europe Standard Time, are mapped to IANA names, and prefixed identifiers such as/mozilla.org/20050126_1/America/New_Yorkare reduced to the IANA part.
VTIMEZONE definitions are ignored: the zone is taken from the TZID name. A TZID that is neither an IANA nor a known Windows name leaves the time floating.
Size limits#
parseICS refuses text that is larger than it expects, so a file built to exhaust memory is stopped early.
| Limit | Default | Applies to |
|---|---|---|
maxLength |
67,108,864 (64 MiB of characters) | The length of the text |
maxEvents |
250,000 | VEVENT blocks, before recurrence is expanded |
import { InputLimitError, parseICS } from '@birchui/calendar';
try {
const events = parseICS(text, { limits: { maxEvents: 20_000 } });
} catch (error) {
if (error instanceof InputLimitError) {
console.warn(error.code, error.limit, error.actual);
}
}Pass only the limits you want to change. Infinity turns one off. The error's code is 'input-too-long' or 'too-many-events'. The defaults are exported as DEFAULT_ICS_LIMITS.
Export a file#
toICS(events, options?) returns the full text of a VCALENDAR. To offer it as a download:
import { toICS } from '@birchui/calendar';
function downloadCalendar(fileName: string) {
const ics = toICS(calendar.getEvents(), { calendarName: 'Support team' });
const blob = new Blob([ics], { type: 'text/calendar;charset=utf-8' });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = fileName;
link.click();
URL.revokeObjectURL(url);
}
document.querySelector('#export-ics')!.addEventListener('click', () => downloadCalendar('support-team.ics'));To export only what is on screen, pass the masters of the visible occurrences:
const { start, end } = calendar.getRange();
const visibleIds = new Set(calendar.getOccurrences(start, end).map((occurrence) => occurrence.id));
const ics = toICS(calendar.getEvents().filter((event) => visibleIds.has(String(event.id))));| Option | Default | Description |
|---|---|---|
calendarName |
none | Written as X-WR-CALNAME, which most apps use as the name of an imported calendar. |
productId |
'-//Birch UI//Birch UI Event Calendar//EN' |
The PRODID line. |
timestamp |
now | The DTSTAMP of every event. Pass a fixed Date for reproducible files and snapshot tests. |
defaultDuration |
one hour | Duration in milliseconds for timed events without an end. |
What is written#
Given these events:
const ics = toICS(
[
{
id: 'standup',
title: 'Support team stand-up',
start: '2026-12-01T09:00',
end: '2026-12-01T09:15',
timeZone: 'America/Chicago',
rrule: 'FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR;UNTIL=20270331T090000',
exdates: ['2026-12-25T09:00'],
},
{ id: 'holiday', title: 'Office closed', start: '2026-12-25', allDay: true },
{
id: 'visit',
title: 'Site visit, Northgate',
start: '2026-12-08T19:00:00Z',
end: '2026-12-08T21:30:00Z',
location: 'Northgate Clinic; 1450 Northgate Blvd',
description: 'Bring the\nreplacement filters',
},
],
{ calendarName: 'Support team', timestamp: new Date('2026-11-26T12:00:00Z') },
);toICS produces:
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Birch UI//Birch UI Event Calendar//EN
CALSCALE:GREGORIAN
X-WR-CALNAME:Support team
BEGIN:VEVENT
UID:standup
DTSTAMP:20261126T120000Z
DTSTART;TZID=America/Chicago:20261201T090000
DTEND;TZID=America/Chicago:20261201T091500
SUMMARY:Support team stand-up
RRULE:FREQ=WEEKLY;UNTIL=20270331T140000Z;BYDAY=MO,TU,WE,TH,FR
EXDATE;TZID=America/Chicago:20261225T090000
END:VEVENT
BEGIN:VEVENT
UID:holiday
DTSTAMP:20261126T120000Z
DTSTART;VALUE=DATE:20261225
DTEND;VALUE=DATE:20261226
SUMMARY:Office closed
END:VEVENT
BEGIN:VEVENT
UID:visit
DTSTAMP:20261126T120000Z
DTSTART:20261208T190000Z
DTEND:20261208T213000Z
SUMMARY:Site visit\, Northgate
DESCRIPTION:Bring the\nreplacement filters
LOCATION:Northgate Clinic\; 1450 Northgate Blvd
END:VEVENT
END:VCALENDARNotes:
- Zoned events are written with
TZID, instants without a zone in UTC, floating times as floating and all-day events withVALUE=DATE. A missing all-dayendbecomes the next day. - A floating
UNTILin a zoned series is converted to UTC, as RFC 5545 requires (20270331T090000in Chicago becomes20270331T140000Z). - Detached occurrences, the events with
recurrenceIdandrecurringEventIdcreated when someone moves a single occurrence, are written asRECURRENCE-IDoverrides under the master'sUID, and their dates are not repeated asEXDATEs. Edited series therefore open correctly in other calendar apps. COLORis written only for named CSS colors such asteal, because the property does not allow hex values.- Lines are folded at 75 octets and joined with CRLF.
- Your other custom fields are not exported.
toICS refers to zones by TZID name and does not write VTIMEZONE blocks. Google Calendar, Outlook and Apple Calendar resolve IANA names without them.
Server-side and worker use#
Both functions are pure and never touch the DOM, and importing the package is safe on a server, so they run anywhere JavaScript does. A Node.js route that serves a subscribable feed:
import { toICS } from '@birchui/calendar';
app.get('/calendars/:teamId.ics', async (request, response) => {
const team = await db.teams.find(request.params.teamId);
const events = await db.events.forTeam(team.id);
response.type('text/calendar; charset=utf-8').send(toICS(events, { calendarName: `${team.name} schedule` }));
});Next steps#
- Recurrence explains detached occurrences.
- Time zones covers floating, zoned and UTC times.