Generate Occurrences from a Recurrence Pattern
With Aspose.Email, it is possible to generate occurrences using a recurrence pattern. This article explains how, how to generate the next occurrence and get user friendly item descriptions. Occurrences from a MAPI calendar recurrence pattern can be generated using Aspose.Email. The following code snippet shows you how to generate occurrences from recurrence patterns.
Calculate the Next Occurrence or n Next Occurrences
To get the “next” occurrence, use the GenerateOccurrences method with the parameter nNextOccurrences=1. The following code snippet shows you how to generates 20 occurrences by using nNextOccurrences = 20. The output of the code below is as follows:
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
RecurrencePattern recurrencePattern = new RecurrencePattern(); | |
recurrencePattern.StartDate = new DateTime(1997, 9, 10, 9, 0, 0); | |
RecurrenceRule rule = recurrencePattern.RRules.Add(); | |
rule.Frequency = Frequency.Monthly; | |
rule.Count = 20; | |
rule.Interval = 18; | |
rule.ByMonthDay.Add(new int[] { 10, 11, 12, 13, 14, 15 }); | |
DateCollection expectedDates = recurrencePattern.GenerateOccurrences(20); | |
Console.WriteLine("expectedDates.Count = " + expectedDates.Count); | |
foreach (DateTime date in expectedDates) | |
{ | |
Console.WriteLine("DateTime = " + date); | |
} |
Get User Friendly Text for a Recurrence
User friendly text for a rule can be obtained using the FriendlyText property as shown below. The output of the code will be: “Recur every month on the 1st and 1st from end day(s) of the month for a maximum of 2 occurrences.”. The following code snippet shows you how to get user friendly text for a recurrence.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
RecurrenceRule rule = new RecurrenceRule(); | |
rule.Frequency = Frequency.Monthly; | |
rule.Count = 2; | |
rule.ByMonthDay.Add(1); | |
rule.ByMonthDay.Add(-1); | |
Console.WriteLine(rule.FriendlyText); |