Skip to content

Instantly share code, notes, and snippets.

Last active May 14, 2024 10:16
Show Gist options
  • 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);
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")))) {
//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("Lugar:", event.location);
console.log("Organizer:", event.organizer);
console.log("Atendees:", event.attendees);
console.log("Summary:", event.summary);
console.log("Description", event.description);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment