Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Decimal to Hours Minutes and Seconds in C# .Net

Tags:

c#

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?

like image 941
user2711213 Avatar asked Aug 23 '13 13:08

user2711213


People also ask

How do you convert decimal minutes to minutes and seconds?

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.

How do you convert numbers into hours minutes and 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 ).


2 Answers

You can use the TimeSpan.FromMinutes(minutesInDouble), pass the above value in double format. For more information - check MSDN link here

like image 154
Bhalchandra K Avatar answered Sep 19 '22 15:09

Bhalchandra K


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
like image 35
Tim Schmelter Avatar answered Sep 19 '22 15:09

Tim Schmelter