Search code examples
c#datetimehour

.Net - Time of the day


I am working on an application that needs to set rules for periods of time. The company has different branches, each branch can set its own rules (i.e a branch starts work at 8.30 am, ends work at 17.30 pm, with 30 minutes pause for lunch; another branch start at 9.00, ends at 19.00 with 1 hour pause...)

So I need to define a class (let's call it WorkingDayDefinition for the moment) where start and end are not actually a DateTime, because they are not referred to any specific day in particular. At the moment the only option I see in C# is using Timespan for setting a duration from the beginning of the day, so that 8.30 pm would be TimeSpan(8,30,0) to be added to the Day part of whichever day.

Is this a best practice in C#?

I searched for third parties libraries that could help me, but so far my best bet is this one:

http://www.codeproject.com/Articles/168662/Time-Period-Library-for-NET

that is not strictly what I need


Solution

  • You could use Noda Time. It provides a LocalTime (see here):

    LocalTime is an immutable struct representing a time of day, with no reference to a particular calendar, time zone or date.

    For 8.30 you would do something like:

    LocalTime openingAt = new LocalTime(8, 30);