Extract from Timespan Totalhours and Minutes

Viewed 10290

I get from TimeSpan.TotalHours 24,75 hours. How can I convert this to get the full and not roundes hours (=24) plus the minutes (0,75hours = 45 minutes)? So the result should be hours = 24 and minutes = 45

I tried to convert it to string and make substring but I would like to know if there is a better way than this.

string test = Reststunden.TotalHours.ToString().Substring(Reststunden.TotalHours.ToString().IndexOf(","),3).Replace(",", "");
double Minuten = Convert.ToInt16(test) * 0.6;
3 Answers

I needed to display the total hours and minutes in the following format HH:mm. With the help of the other answers I came to the following result:

var end = DateTime.Now;
var duration = end - StartUpTime;
var h = (int)(duration.TotalMinutes / 60);
var m = (int)(duration.TotalMinutes % 60);
var text = $"{h:D2}:{m:D2}";
Related