Skip to main content

serializeICal

Serializes an array of CalendarEvent objects into an iCalendar text string. Output is ready to write into a Blob or send to a server.

Usage

function serializeICal(events: CalendarEvent[]): string;

Each event is mapped as follows:

CalendarEvent fieldiCal output
idUID
start, endDTSTART, DTEND (UTC, YYYYMMDDTHHmmssZ)
allDay: trueDTSTART;VALUE=DATE, DTEND;VALUE=DATE
textSUMMARY (escaped)
descriptionDESCRIPTION (escaped)

Recurrence rules, attendees, alarms, and timezone overrides are not serialized.

Example

Export the current calendar state as a downloadable .ics file:

<script>
import { Calendar, serializeICal } from "@svar-ui/svelte-calendar";

let api = $state();

function exportIcal() {
const ics = serializeICal(api.getEvents());
const blob = new Blob([ics], { type: "text/calendar" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "calendar.ics";
a.click();
URL.revokeObjectURL(url);
}
</script>

<Calendar bind:this={api} events={[]} date={new Date()} />
<button onclick={exportIcal}>Export .ics</button>
  • parseICal — counterpart that turns .ics text into events.
  • getEvents — read the live event list before serializing.
  • Import and export — full round-trip and JSON alternatives.