I have an ASP.Net calendar feature which allows users to add events and
configure whether or not they repeat at various frequencies (i.e, daily,
weekly, monthly, Sat/Sun, etc.). What I’m looking for is some C# code that
will calculate a date of “Every Other Week” based on a starting date and
ending date.
Example: If a user enters an event dated July 7, 2012 and would like it
to repeat every other week until July 31, 2012, the code will return the
following dates:
July 7, 2012
July 14, 2012
July 21, 2012
July 28, 2012
and one more functionality is there to selection of days in week(i.e, mon,tue,wed..).
Any help would be greatly appreciated.
You probably want a function that takes two DateTime objects (The start date and the end date) and some interval in between events. You can then use the DateTime.AddDays(double value) function to generate all of the in-between dates. To get every other week, your interval would be 14. You would keep adding 14 days until the date you got was after your end date.
You’ll need to be a bit careful if your start and end dates contain a Time component as well. For example, if:
Then above function would not include a July 28 date in the list because 21 days after July 7th 10:30 AM is July 28th 10:30 AM which would not satisfy the conditional in
while(tempDate <= end)because end occurs at 9:00 AM