I want to convert minutes to hours:minutes. I could do
int minutes = 75 ;
string time = TimeSpan.FromMinutes((double)minutes).ToString(@"hh\:mm");
This returns 01:15. However, if I do
int minutes = 1515;
string time = TimeSpan.FromMinutes((double)minutes).ToString(@"hh\:mm");
it also returns 01:15 because it overflows into days. I don't want to do days (dd), what I want is just hours and minutes. So for 1515 minutes, the desired result would be 25:15.
Anyway to accomplish this with TimeSpan? Or any other method for that matter.
This should work:
We don't need TimeSpan to calculate hours and minutes so we just use division and remainder to get the two parts. Dividing two integers always produces an integer, so the partial minutes are dropped by it.
Then we format the number using the D2 format so the number is padded with zeros is needed (e.g. "01"). (docs for number formatting)