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?
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.
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 ).
TimeSpan.FromHours
:
double value;
TimeSpan t = TimeSpan.FromHours(value);
Example:
Console.WriteLine(TimeSpan.FromHours(1.4)); // prints 01:24:00 to the console.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With