Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decimal Hour into Time?

Tags:

c#

asp.net

I have an hour field in a database like 1.4, 1.5, 1.7 that I need to convert back to HH:MM. What is the easiest way to do this?

like image 719
Tony Borf Avatar asked Jul 06 '09 15:07

Tony Borf


People also ask

How do you convert decimal time to hours?

If you have time value in decimals and you want to convert it into hours, all you have to do is multiply it by 24. Note that 24 here represents 24 hours, the number of hours in a day.

How do you convert hours into Time?

To convert time to a number of hours, multiply the time by 24, which is the number of hours in a day. To convert time to minutes, multiply the time by 1440, which is the number of minutes in a day (24*60). To convert time to seconds, multiply the time time by 86400, which is the number of seconds in a day (24*60*60 ).


2 Answers

TimeSpan.FromHours:

double value;
TimeSpan t = TimeSpan.FromHours(value);

Example:

Console.WriteLine(TimeSpan.FromHours(1.4)); // prints 01:24:00 to the console.
like image 196
jason Avatar answered Nov 13 '22 07:11

jason


The HH (hour) figure is the number before the decimal.

To get the minutes figure, subtract the whole number portion so you only have the amount after the decimal, then multiply the fraction by 60 to get the minutes.

like image 22
Bork Blatt Avatar answered Nov 13 '22 07:11

Bork Blatt