简体   繁体   中英

Type to store time in C# and corresponding type in T-SQL

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 DateTime or TimeSpan type in C# to store 9 PM is up to taste. Personally, I'd use DateTime , leaving the date component empty, since that's semantically closer to what you want. (A TimeSpan is designed to hold time intervals , such as "21 hours".)

The documentation supports both options. This is from the documentation of TimeSpan :

The TimeSpan structure can also be used to represent the time of day, but only if the time is unrelated to a particular date.

On the other hand, the MSDN article Choosing Between DateTime, DateTimeOffset, and TimeZoneInfo mentions the following:

The DateTime structure is suitable for applications that do the following:
* Work with dates only.
* Work with times only.
[...]


T-SQL

SQL Server has a time data type.

In C# there is not a type to hold only a time. There is TimeSpan , but it's intended to keep a period of time and not really a component of a DateTime (ie hours and minutes) only.

Starting with SQL Server 2008 there is a time type ( Using Date and Time Data ) that does only store a time component.

EDIT: Misread your question at first. TimeSpan is exactly what you're looking for and it can be stored in a time type with SQL 2K8.

In C# you'd probably want to use a TimeSpan structure if you just wanted to store a time interval . However, you seem to want to appear to store a start-time and an end-time, which would require storing two values. You could, therefore, use two TimeSpans (based on, say, number of minutes from midnight to represent the time) or you could just use two DateTime values and throw away the date component.

As has been noted, SQL Server 2008 has a Time datatype, but this isn't available in earlier versions which only have DateTime . You could also just store an Int representing number of minutes past midnight which can be easily converted to a TimeSpan ( TimeSpan interval = TimeSpan.FromMinutes(60) ).

Timespan in c# is how you manipulate time intervals. Contrary to what other posters are saying i don't think the Time data type is correct for storing time intervals in SQL, unless you actually want to store the start time and end time and not the time interval (ie 1 hour in you example). It is for storing a time of day, a bit like a DateTime but with no date. When i want to store a time interval in SQL I just use an int and then have it represent a unit of time appropriate to what I am trying to do (egminutes, seconds, milliseconds etc. )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM