c# - calender repeat at various frequencies -


i have asp.net calendar feature allows users add events , configure whether or not repeat @ various frequencies (i.e, daily, weekly, monthly, sat/sun, etc.). i'm looking c# code calculate date of "every other week" based on starting date , ending date.

example: if user enters event dated july 7, 2012 , repeat every other week until july 31, 2012, code return following dates:

july 7, 2012 july 14, 2012 july 21, 2012 july 28, 2012

and 1 more functionality there selection of days in week(i.e, mon,tue,wed..).

any appreciated.

you want function takes 2 datetime objects (the start date , end date) , interval in between events. can use datetime.adddays(double value) function generate of in-between dates. every other week, interval 14. keep adding 14 days until date got after end date.

//create list of datetimes including start date, specified //number of days in between each item in list. public static ilist<datetime> getrepeatingevents(datetime start, datetime end, int intervalindays) { list<datetime> allevents = new list<datetime>(); allevents.add(start); //make sure start date included in list of dates! var tempdate = start; while (tempdate <= end) //less or equals means end date added { tempdate = tempdate.adddays(intervalindays); allevents.add(tempdate); } return allevents; } 

you'll need bit careful if start , end dates contain time component well. example, if:

  • your start date july 7, 2012 10:30 am
  • your end date july 28, 2012 9:00 am
  • your interval 1 week (7 days)

then above function not include july 28 date in list because 21 days after july 7th 10:30 july 28th 10:30 not satisfy conditional in while(tempdate <= end) because end occurs @ 9:00 am


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -