digitally_inspired
digitally_inspired

Reputation: 777

ICAL.NET Recurring Elements: every Tuesday(weekly), Every 2nd, last Monday(monthly) from a start date to end date

The ical attachment (.ics) works for every case except recurring weekly and once in a month cases. I'm trying to create a weekly recurrence for one of the weekday and also a monthly recurrence for last Tuesday/2nd Tuesday of every month. When I open the .ics file, it says "The operation failed".

Weekly:

evnt.Start = new CalDateTime(start);
evnt.End = new CalDateTime(new DateTime(evnt.Start.Year, evnt.Start.Month, evnt.Start.Day, end.Hour, end.Minute, end.Second));
rrule = new RecurrencePattern(FrequencyType.Weekly, 1);
rrule.ByDay.Add(new WeekDay(DayOfWeek.Monday));
rrule.Until = end;
evnt.RecurrenceRules = new List<RecurrencePattern> { rrule };

Monthly:

evnt.Start = new CalDateTime(start);
evnt.End = new CalDateTime(new DateTime(evnt.Start.Year, evnt.Start.Month, evnt.Start.Day, end.Hour, end.Minute, end.Second));
rrule = new RecurrencePattern(FrequencyType.Monthly,1);
rrule.Until = end;
rrule.ByDay.Add(new WeekDay(DayOfWeek.Sunday,FrequencyOccurrence.FifthToLast));
evnt.RecurrenceRules = new List<RecurrencePattern> { rrule };

Please let me know what I'm doing wrong.

Upvotes: 0

Views: 3124

Answers (2)

digitally_inspired
digitally_inspired

Reputation: 777

With the cues I got from @matis and @anmari I figured the root cause.

When you create a recurring appointment with a specific weekday, both start and end date should match the same weekday. Here is a working sample occurring on 2nd Wednesday of every month.

BEGIN:VCALENDAR
METHOD:PUBLISH
PRODID:-//github.com/rianjs/ical.net//NONSGML ical.net 4.0//EN
VERSION:2.0
BEGIN:VEVENT
ATTENDEE;CN="Last, First":mailto:[email protected]
CREATED:20200415T164742
DESCRIPTION: Cal Description
DTEND;TZID=Eastern Standard Time:20200513T110000
DTSTAMP:20200415T204742Z
DTSTART;TZID=Eastern Standard Time:20200513T103000
LOCATION:Meeting location
ORGANIZER;CN="user, Admin";SENT-BY="mailto:[email protected]":
RRULE:FREQ=MONTHLY;COUNT=13;BYDAY=2WE
SEQUENCE:0
SUMMARY:Title
UID:1913d514-696e-4237-bc3c-c1d073eacced
BEGIN:VALARM
ACTION:DISPLAY
DESCRIPTION:Reminder
TRIGGER:-PT15M
END:VALARM
END:VEVENT
END:VCALENDAR

Upvotes: 1

Matis
Matis

Reputation: 631

Could you please provide iCalendar data that your code will output ? And possibly show us what start and end are.

I'm trying to create a weekly recurrence for one of the weekday

For this, the iCalendar data should look like this:

FREQ=WEEKLY;BYDAY=MO;INTERVAL=1;UNTIL=20200515T220000Z

I picked Monday as the day, and set an end date one month ahead, as you too, provided some end.


I'm trying to create a monthly recurrence for last Tuesday/2nd Tuesday of every month.

As @anmari pointed out, you have a Sunday in your code but you want a recurring Tuesday.

For this the iCalendar should look like this:

FREQ=MONTHLY;BYDAY=2TU,-1TU;INTERVAL=1;UNTIL=20200515T220000Z

or even like this (with BYSETPOS):

FREQ=MONTHLY;BYSETPOS=2,-1;BYDAY=TU;INTERVAL=1;UNTIL=20200515T220000Z

Upvotes: 1

Related Questions