I am using the nuget package Ical.Net 4.2.0 in my C# project. I've created a recurring calendar event and need to identify the start DateTime for each occurrence. I query the calendar with GetOccurrences() like this:
var now = DateTime.Parse("2021-09-16T10:00:00");
var later = now.AddHours(1);
//Repeat daily for 5 days
var rrule = new RecurrencePattern(FrequencyType.Daily, 1) { Count = 5 };
var e = new CalendarEvent
{
Start = new CalDateTime(now),
End = new CalDateTime(later),
RecurrenceRules = new List<RecurrencePattern> { rrule },
};
var calendar = new Calendar();
calendar.Events.Add(e);
var startSearch = new CalDateTime(DateTime.Parse("2021-09-16T00:00:00"));
var endSearch = new CalDateTime(DateTime.Parse("2021-09-21T23:59:59"));
var occurrences = calendar.GetOccurrences(startSearch, endSearch)
.Select(o => o.Source)
.Cast<CalendarEvent>()
.ToList();
occurrences
contains five events, but looking for the "start" date, I can only find the event start date. Where can I find the start dates for each occurrence of a recurring event?
You get them in the actual
Occurrence
object returned byGetOcurrences
(which you are discarding and actually selecting the sourceCalendarEvent
, which is the same for all occurrences), in thePeriod.StartTime
property (which is anIDateTime
, which you can convert to .NET datetime objects)To get all start date times (as
DateTimeOffset
object):See it in action