I have an minutes field in a database like 138.34 that I need to convert back to HH:MM:SS What is the easiest way to do this?
When a time is expressed as a decimal that includes hours, the hours remain the same upon conversion. Multiply the remaining decimal by 60 to determine the minutes. If that equation produces a decimal number, multiply the decimal by 60 to produce the seconds.
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 ).
You can use the TimeSpan.FromMinutes(minutesInDouble)
, pass the above value in double format.
For more information - check MSDN link here
Use the TimeSpan
structure:
var timeSpan = TimeSpan.FromMinutes(138.34);
int hh = timeSpan.Hours;
int mm = timeSpan.Minutes;
int ss = timeSpan.Seconds;
Result:
Console.WriteLine("Hours:{0} Minutes:{1} Seconds:{2}", hh, mm, ss);
// Hours:2 Minutes:18 Seconds:20
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