I would like to know how to store time in C# and T-SQL. I know that both of them provide a DateTime type but I just need to store a time. For instance:
var startTime = 9PM;
var endTime = 10PM;
And then store/retrieve this values from a database. Thanks in advance.
Francesco
C#
Whether to use a
DateTimeorTimeSpantype in C# to store9 PMis up to taste. Personally, I’d useDateTime, leaving the date component empty, since that’s semantically closer to what you want. (ATimeSpanis designed to hold time intervals, such as “21 hours”.)The documentation supports both options. This is from the documentation of TimeSpan:
On the other hand, the MSDN article Choosing Between DateTime, DateTimeOffset, and TimeZoneInfo mentions the following:
T-SQL
SQL Server has a
timedata type.