Skip to content

Instantly share code, notes, and snippets.

@mogarick
Last active January 27, 2021 14:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mogarick/9246085 to your computer and use it in GitHub Desktop.
Save mogarick/9246085 to your computer and use it in GitHub Desktop.
Getting UTC timestamps of iCal with Timezone using the mozilla-comm/ical.js lib
//Please note I'm also using underscore each function for the iterations
//iCal -> jsCal
var icalEvents = ICal.parse(iCalStringData);
//jsCal->Component
var comp = new Component(icalEvents[1]);
//Get all VTIMEZONE iCalendar components
var vtimezones = comp.getAllSubcomponents("vtimezone");
//Add all timezones in iCalendar object to TimezonService
//if they are not already registered.
_.each(vtimezones, function (vtimezone) {
if (!(ICAL.TimezoneService.has(
vtimezone.getFirstPropertyValue("tzid")))) {
ICAL.TimezoneService.register(vtimezone);
}
});
//Get all VEVENT iCalendar components
var vevents = comp.getAllSubcomponents("vevent");
var events = [];
var eventCount = 1;
_.each(vevents, function (vevent) {
var event = new Event(vevent);
//Get the datetime info in UTC format with appropiate offset
var startDate = new Date(event.startDate.toUnixTime() * 1000);
var endDate = new Date(event.endDate.toUnixTime() * 1000);
console.log(eventCount, " - - - - - - - - - - - - - - - - - - ");
console.log("UID:", event.uid);
console.log("Recurrent?", event.isRecurring());
console.log("Start:", startDate);
console.log("End:", endDate);
//This also depends on the correct timezone to be present in TimezoneService
console.log("Duration:",event.duration.toString());
console.log("Lugar:", event.location);
console.log("Organizer:", event.organizer);
console.log("Atendees:", event.attendees);
console.log("Summary:", event.summary);
console.log("Description", event.description);
console.log("\n");
events.push(event);
eventCount++;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment