Skip to main content

parseICal

Parses an iCalendar (.ics) text payload into an array of CalendarEvent objects. start, end, and allDay are populated from DTSTART and DTEND; SUMMARY becomes text and DESCRIPTION becomes description.

Signature

function parseICal(ics: string): CalendarEvent[];
ParameterTypeDescription
icsstringRaw iCalendar text from a .ics file.

Returned events:

FieldTypeDescription
idstring | numberTaken from UID; coerced to number when numeric. A random id is used when missing.
startDateParsed from DTSTART. UTC Z values produce a UTC Date; floating values are local.
endDateParsed from DTEND. Falls back to start when DTEND is absent.
allDaybooleanSet to true when DTSTART is DATE-only (YYYYMMDD).
textstringUnescaped SUMMARY value (when present).
descriptionstringUnescaped DESCRIPTION value (when present).

Importing on upload

import { useState } from "react";
import { Calendar, parseICal } from "@svar-ui/react-calendar";

function App() {
const [data, setData] = useState([]);

function importIcal(e) {
const file = e.target.files?.[0];
if (!file) return;
const reader = new FileReader();
reader.onload = ev => {
setData(parseICal(ev.target.result));
};
reader.readAsText(file);
}

return (
<>
<input type="file" accept=".ics" onChange={importIcal} />
<Calendar events={data} date={new Date()} />
</>
);
}
  • serializeICal — counterpart that turns events back into .ics text.
  • events — array consumed by the Calendar after parsing.
  • Import and export — full round-trip patterns and limitations.