I have a Time column in a database table. The date is not important, we just want a time in the day. What type would be best to represent it in C#? I was going to use a DateTime, but I don't like the idea of having a date.
In C# a DateTime data type is a struct type that represents an instant of time.
There are two ways to initialize the DateTime variable: DateTime DT = new DateTime();// this will initialze variable with a date(01/01/0001) and time(00:00:00). DateTime DT = new DateTime(2019,05,09,9,15,0);// this will initialize variable with a specific date(09/05/2019) and time(9:15:00).
The DateTimeOffset structure includes a DateTime value, together with an Offset property that defines the difference between the current DateTimeOffset instance's date and time and Coordinated Universal Time (UTC).
DateTime date1 = new DateTime(2018, 7, 15, 08, 15, 20); DateTime date2 = new DateTime(2018, 8, 17, 11, 14, 25); Now, get the difference between two dates. TimeSpan ts = date2 - date1; Get the result i.e. the difference in hours.
While the other answers are mostly correct that a TimeSpan
is the only built-in type that will work, it's important to realize that there are distinct differences between an "elapsed measurement of time" and a "time of day".
The most obvious difference is that a time of day must be less than 24 hours. A TimeSpan
object can cover much more than that.
Another difference is that a TimeSpan
type can be negative. This represents moving backwards in time. A negative value would be meaningless as a time-of-day.
And finally, a time-of-day includes any concept of daylight saving time that might apply to the time zone in which it was taken. So you can't think of it as "elapsed time since midnight".
4:00
has only elapsed 3 hours since midnight.4:00
has actually elapsed 5 hours since midnight.So if you use TimeSpan
as a time-of-day, you need to be aware of these issues. .NET doesn't have a built-in type for a time-of-day, so this is an acceptable compromise, even though it's in violation of it's own design.
Even the .NET Framework itself makes this compromise. For example:
DateTime
class has a TimeOfDay
property that returns a TimeSpan
.time
type in SQL Server, it will be a TimeSpan
when returned through the .NET SQL Client.The MSDN Reference Documentation has this to say about the TimeSpan
type:
The TimeSpan structure can also be used to represent the time of day, but only if the time is unrelated to a particular date. Otherwise, the DateTime or DateTimeOffset structure should be used instead.
That is basically another way of saying what I covered in my third point above about DST.
However, if you are not interested in making compromises in your design and would like a real time-of-day type, then take a look at the Noda Time library.
LocalTime
type, which represents a time of day. This is the direct answer to the question that was asked. Duration
type, which represents an elapsed measure of time.Period
type, which represents a positional movement on a calendar - which is something else that TimeSpan
can't do. For example, "3 years and 5 months" would be a Period
value.Offset
type, which is similar to a Duration
, but is used as an offset from UTC for time zones. It has a range limited to that purpose.While some could say that TimeSpan
is more versatile since it can handle all of these, the truth is that it allows you to get into trouble. By separating the types, you get safety and consistency.
Alternatively, you may consider the System.Time
package available from Microsoft as part of CoreFX Lab. This package contains implementations of a time-only type called Time
, and a date-only type called Date
. You will need to use the dotnet-corefxlab
MyGet feed to import this package.
You could use a TimeSpan structure to represent a time in .NET.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With