I need a way to store and manipulate a date range using C# and storing the data in a SQL database. Is storing two DateTimes the best way to go about this?
For example I need to employees to be able to select the duration they spent working on a specific project by selecting the start date and end date using a DatePicker.
I have the following further requirements:
- I need to support half days at start
and/or end of the duration. - I need to be able to calculate the number of
days between the two dates (as a
double where 0.5 is half a day). - I need to be able to calculate the
number of business days between the
two dates (as a double). - The time span needs to be displayed on a
jquery calendar. -
The minimum
duration is half a day.A Date Range of 1/2 Day From 24th May to a full day 27th May:
2011-05-24 12:00:00.000 => 2011-05-28 00:00:00.000A Date Range of Full Day From 24th May to a 1/2 day 27th May:
2011-05-24 00:00:00.000 => 2011-05-27 12:00:00.000A Half Day on 24th May:
2011-05-24 12:00:00.000 => 2011-05-25 00:00:00.000A Full Day on 24th May:
2011-05-24 00:00:00.000 => 2011-05-25 00:00:00.000
Does this representation make sense? Should I rather look at storing a DateTime for the StartDate and a TimeSpan taking into account my requirements?
Edit: also
Does my representation of end date make sense? So that 2nd of may will be saved as ‘2011-05-03 00:00:00.000’ because that is when the duration ends. Bearing this in mind I’ll need to subtract a day from the end date when displaying this in a calendar..
I suggest to save the start and end date to your database. The difference can always be calculated.
The critical aspect of date ranges is how to handle the boundaries. You can use a mapper for the start/end date to ensure correct time calculations (Inside/Touching):
Check out the article Time Period Library for .NET (section Calendar Time Periods).