I want to make a recurring event, like something that occurs every tuesday and thursday at 8:30 A.M. Because it is recurring, I don't want there to be month, day, and year properties involved--just the hour and minute. Should I still use DateTime, or is there another way to best represent this data?
-
Here is a [good post](http://stackoverflow.com/questions/123793/design-question-how-would-you-design-a-recurring-event-system) describing a good solution. – Mike Perrenoud Aug 20 '12 at 16:12
-
1Don't re-invent the wheel. Use [Quartz.Net](http://quartznet.sourceforge.net/features.html). If that's too much, check [its source](https://github.com/quartznet/) for ideas. – Sumo Aug 20 '12 at 16:22
4 Answers
Check out ICal on Wikipedia and corresponding RFC 5545 for inspirations on what information you need to create good data structure for recurrent event.
Daily until December 24, 1997:
DTSTART;TZID=America/New_York:19970902T090000 RRULE:FREQ=DAILY;UNTIL=19971224T000000Z
DateTime
or TimeSpan
would work fine for "time" portion. Try to first write code that uses your data structure and see what set of methods works best as DataTime
and TimeSpan
have slightly different APIs.

- 1
- 1

- 98,904
- 14
- 127
- 179
-
Not the easiest to work with, but it is (kinda) standardized and it does have everything that you would need (plus a lot you won't). – Servy Aug 20 '12 at 16:20
DateTime doesn't make much sense. I would simply have a class with exactly the fields you need:
public class DayAndTime {
public DayOfWeek Day { get; set; }
public int Hour { get; set; }
public int Minute { get; set; }
public int Second { get; set; }
}
Edit after answer was accepted, because I thought it was interesting:
You could also add a method to that class to get the next time something needs to run like this:
public DateTime NextDate {
get {
DateTime today = DateTime.Today;
DateTime now = DateTime.Now;
int daysUntilWeekday = ((int)Day - (int)today.DayOfWeek + 7) % 7;
DateTime nextWeekDay = today.AddDays(daysUntilWeekday);
var nextTime = nextWeekDay.AddSeconds(Second).AddMinutes(Minute).AddHours(Hour);
if (nextTime < DateTime.Now) {
return nextTime.AddDays(7);
}
else {
return nextTime;
}
}
}

- 23,318
- 5
- 58
- 81
I'd use a TimeStamp
and have it offset from midnight of Day 0, which is Sunday.

- 50,774
- 20
- 136
- 184
Should I still use DateTime, or is there another way to best represent this data?
Yes DateTime is best when you dealing with DateTime Format. It will have information about Day of Week when you want to do some recurring event.
You can use Timer to check if its tuesday or thursday 8:30 A.M or Not.

- 47,018
- 22
- 121
- 208