kewisch / ical.js

Javascript parser for ics (rfc5545) and vcard (rfc6350) data
https://kewisch.github.io/ical.js/
Mozilla Public License 2.0
1k stars 138 forks source link

List Events? Examples? #222

Closed Meekohi closed 8 years ago

Meekohi commented 8 years ago

So... can I use this library to get a list of all the events for a month in an .ics file? Any example code?

Meekohi commented 8 years ago

This worked for me. Would be nice to have a simplified example like this in the README or documentation along with whatever caveats are needed (handling recurring events, etc):

var url = "https://calendar.google.com/calendar/ical/"+email+"/public/basic.ics";
  request(url,function(err, resp, calendarData){
    var jcal = icaljs.parse(calendarData);
    var vcal = new icaljs.Component(jcal);
    var vevents = vcal.getAllSubcomponents("vevent");
    return _.map(vevents,function(vevent){
      return {
        name: vevent.getFirstPropertyValue("summary"),
        starttime: moment(vevent.getFirstPropertyValue("dtstart")).format(),
        endtime: moment(vevent.getFirstPropertyValue("dtend")).format(),
        description: vevent.getFirstPropertyValue("description")
      };
    });
  });
kewisch commented 8 years ago

How about this page? https://github.com/mozilla-comm/ical.js/wiki/Parsing-basic-iCalendar

j-norwood-young commented 5 years ago

I noticed that moment(vevent.getFirstPropertyValue("dtstart")).format() was dropping the hours, minutes and seconds in the solution from @Meekohi. Adding .toString() to the icaltime components solves this problem.

...
starttime: moment(vevent.getFirstPropertyValue("dtstart").toString()).format(),
endtime: moment(vevent.getFirstPropertyValue("dtend").toString()).format(),
...