Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a meaningfull datatype to save hours

What is a good data-type for saving hours in .net?

Is it better to use the decimal type or is the double data-type more appropriate. With hours I mean values such as:

  • 2 for two hours
  • 1.5 for 90 minutes
  • 8.25 for 8 hours and 15 minutes.
like image 775
HCL Avatar asked Nov 01 '10 10:11

HCL


1 Answers

A good way to represent a number of hours is to use a TimeSpan:

TimeSpan hours = TimeSpan.FromHours(2);

Given the choice between decimal or double I'd probably go for double as there is typically no expectation that the amount of time is represented exactly. If you need an exact decimal representation of your fractional number of hours (which seems unlikely) then use decimal.

You could also consider storing it as an integer in for example seconds, milliseconds or ticks.

like image 153
Mark Byers Avatar answered Sep 23 '22 16:09

Mark Byers