Is there a way to make a list of time range?
For example:
A list containing:
12:00 to 1:00 pm
1:00 to 2:00 pm
etc…
Where the dividing section is configuration.
I think you have to use datetime and divide it to a certain number(in this case one hour)
Could someone please point me to the right direction or provide me an example?
Thanks in advance!
There’s no built-in type that defines a time-range but it would be pretty easy to create one by combining a
DateTimeand aTimeSpan. For example:You could then build a
List<TimeRange>using a specificDateTimeas the starting point and adding the requiredTimeSpanfor each element. For example, here’s a very basic implementation ofTimeRangeincluding a method calledSplitwhich returns anIEnumerable<TimeRange>based on the currentTimeRangeand the required duration of the sub-ranges.You can then do something like this:
This will give a list of 12 time ranges of 1-hour duration starting from the current time.
** Update **
Note that the above implementation is VERY basic. The
Splitmethod, for example, will happily produce a lits of ranges where the end of the last sub-range is beyond the end of the parent range if the sub duration is not an integral division of the parent range. It would be hard to add checks for this kind of thing, though. The real question is what you want to happen in those kind of scenarios.It would also be very easy to create a static
TimeRange.CreateListmethod that builds aList<TimeRange>without the need for an explicit parent range.