user2711213
user2711213

Reputation: 161

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

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?

Upvotes: 13

Views: 36977

Answers (2)

Tim Schmelter
Tim Schmelter

Reputation: 460018

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

Upvotes: 21

Bhalchandra K
Bhalchandra K

Reputation: 2681

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

Upvotes: 26

Related Questions