Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using DateTime for a duration of time e.g. 45:00

Tags:

c#

I'd just like to know if it is possible to use the DateTime type for durations such as 45:00 (45 minutes) or 120:00 (120 minutes). These values also need to be stored into a Local Sql Server DB. If it is possible, could anyone possibly hint how this could be done using Datetime, or if not just let me know a way it could be done using a different type.

Thank you in advance, Jamie

like image 286
Jamie Mclaughlan Avatar asked Dec 01 '25 06:12

Jamie Mclaughlan


2 Answers

You should use the TimeSpan structure

TimeSpan interval = new TimeSpan(0, 45, 0);
Console.WriteLine(interval.ToString());  

For the database storing part, you could store the property Ticks because a specific constructor for the TimeSpan structure allows to instantiate a new TimeSpan passing the Ticks value

long ticks = GetTimeSpanValueFromDb();
TimeSpan interval = new TimeSpan(ticks);

I wish to add also that you need a BIGINT T-SQL datatype field to store a long NET datatype

like image 184
Steve Avatar answered Dec 03 '25 18:12

Steve


I store durations in seconds in the database and then convert to HH:MM:SS format when comes time to display the data.

like image 40
frenchie Avatar answered Dec 03 '25 20:12

frenchie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!