Skip to content

Instantly share code, notes, and snippets.

Created August 22, 2014 18:58
Show Gist options
  • Save anonymous/b8d39378a9f77fecddbe to your computer and use it in GitHub Desktop.
Save anonymous/b8d39378a9f77fecddbe to your computer and use it in GitHub Desktop.
Getting the first event from a .ics calendar file using DDay.iCal
using System;
using DDay.iCal;
using Newtonsoft.Json;
public class YourClass
{
public string YourMethod()
{
//Download the .ics file using DDay.iCal package from nuget (install-package dday.ical)
var calendar = iCalendar.LoadFromUri(new Uri("http://spacexstats.com/calendar.php?launch=all")).FirstOrDefault();
//Ensure calendar is not null
if (calendar != null)
{
//Get the first event ordered by start date.
var calEvent = calendar.Events.OrderBy(x => x.Start).FirstOrDefault();
//Ensure event is not null
if (calEvent != null)
{
//Will return something like this: "Event found. Summary: AsiaSat 6 Launch. Description: AsiaSat 6 is a communications satellite being launched for Asia Satellite Telecommunications Company Ltd. Once in orbit, it will be renamed AsiaSat 6 / Thaicom 7.. Start Date: Aug 26, 2014 UTC. End Date: Aug 26, 2014 UTC."
return String.Format("Event found. Summary: {0}. Description: {1}. Start Date: {2}. End Date: {3}.", calEvent.Summary, calEvent.Description, calEvent.Start.ToString("MMM dd, yyyy"), calEvent.End.ToString("MMM dd, yyyy"));
}
else
{
return "This calendar does not contain any events.";
}
}
else
{
return "This calendar was not found.";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment