-2

I need to calculate the end date of RRULE :

Weekly on Tuesday and Thursday for 5 weeks:

DTSTART;TZID=US-Eastern:19970902T090000 RRULE:FREQ=WEEKLY;UNTIL=19971007T000000Z;WKST=SU;BYDAY=TU,TH or RRULE:FREQ=WEEKLY;COUNT=10;WKST=SU;BYDAY=TU,TH

==> (1997 9:00 AM EDT)September 2,4,9,11,16,18,23,25,30;October 2

I need to do it in ms SQL. any help would be appreciated.

  • 1
    The details you shared does not explain any thing. Can you share some sample input and expected output for them? Also explain the logic of generating the output? Can you also share whatever code you have written and tell us what exact issue you are facing? – Chetan Jul 06 '20 at 15:25
  • Does this answer your question? [Recurrence Library for date calculations for .Net](https://stackoverflow.com/questions/4517376/recurrence-library-for-date-calculations-for-net) – Murray Foxcroft Jul 06 '20 at 15:31
  • For example: Start Date = 17-07-2020 Meeting Occurrence = 31(Monday to Friday)(Day calculation :Day Value Monday 1 Tuesday 2 Wednesday 4 Thursday 8 Friday 16 Saturday 32 Sunday 64) Count: 2 So send end date should be: 20-07-2020 Expected result = 20-07-2020 If count = 3 So send end date should be: 21-07-2020 Expected result = 21-07-2020 I hope from this you will get some idea. Let me know if you need more information. Thanks, – Pankaj Kadian Jul 06 '20 at 15:32
  • Hello @MurrayFoxcroft, Thanks for sharing the details. I need to implement it in SQL, I mentioned it c# because if I get logic from c# then I can convert it into the SQL. Thanks, Pankaj Kadian – Pankaj Kadian Jul 06 '20 at 15:37
  • The end date of RRULE is in the UNTIL=19971007T000000Z... Note that it may also have no end date. – Murray Foxcroft Jul 06 '20 at 15:52

1 Answers1

0

See following :

DateTime date = new DateTime(2020, 7,17);

DayOfWeek dayOfWeek = date.DayOfWeek;

int addOffset = 0;
//get first tuesday or thursday
switch (dayOfWeek)
{
    case DayOfWeek.Sunday :
        addOffset = 2;
        break;
    case DayOfWeek.Monday:
        addOffset = 1;
        break;
    case DayOfWeek.Tuesday:
        addOffset = 0;
        break;
    case DayOfWeek.Wednesday:
        addOffset = 1;
        break;
    case DayOfWeek.Thursday:
        addOffset = 0;
        break;
    case DayOfWeek.Friday:
        addOffset = 4;
        break;
    case DayOfWeek.Saturday:
        addOffset = 3;
        break;
}
DateTime startDate = date.AddDays(addOffset);
DateTime currentDate = startDate;

int numberOfWeeks = 5;
for (int i = 0; i < numberOfWeeks; i++)
{
    for(int j = 0; j < 2; j++)
    {
        Console.WriteLine(currentDate);
        if (currentDate.DayOfWeek == DayOfWeek.Tuesday)
        {
            currentDate = currentDate.AddDays(2);
        }
        else
        {
            currentDate = currentDate.AddDays(5); //from thursday to tuesday
        }
    }
}
Console.ReadLine();
jdweng
  • 33,250
  • 2
  • 15
  • 20