Giacomo Balli profile picture
Giacomo Balli
The Mobile Guy

Over a decade of mobile experience at your service.
I help business owners leverage mobile technology.

Let's Chat LinkedIn

How to parse ICS calendar file in javascript

To parse an ICS file using JavaScript without libraries, you can follow these general steps:
  1. Read the contents of the ICS file into a string variable. This can be done using XMLHttpRequest or fetch() if the ICS file is hosted on a server, or using the HTML5 FileReader API if the ICS file is selected by the user through a file input.
  2. Split the contents of the ICS file into lines, using the newline character (\n) as the delimiter. This can be done using the JavaScript string method split().
  3. Loop through each line in the array of lines, and check if it matches the start of a new event, indicated by the "BEGIN:VEVENT" line. If a new event is found, create a new object to store the event details
  4. For each event, loop through each line and parse the details using regular expressions. For example, to extract the event summary, you can use the following regular expression: /^SUMMARY:(.*)$/.
  5. Store the event details in the object created in step 3.
  6. When all events have been parsed, return the array of event objects.


Here is some sample code to get you started:
function parseICS(icsString) {
  const lines = icsString.split('\n');
  const events = [];
  let event;

  for (let i = 0; i < lines.length; i++) {
    const line = lines[i].trim();
    if (line === 'BEGIN:VEVENT') {
      event = {};
    } else if (line === 'END:VEVENT') {
      events.push(event);
    } else if (event) {
      const match = /^([A-Z]+):(.*)$/.exec(line);
      if (match) {
        const [, key, value] = match;
        event[key] = value;
      }
    }
  }

  return events;
}



Published: Tue, Feb 28 2023 @ 20:03:56
Back to Blog