 We are excited to announce that the ASP.NET Forums are moving to the new Microsoft Q&A experience. Learn more >

## 9 replies

Last post Nov 09, 2018 11:24 AM by jzero

• ### Converting Minutes to Hours and Minutes

I want to convert minutes to hours and minutes. I have the following code -

I have 11803.228 minutes and it should display 196 hours and 43 minutes  (not 196.72 as original typo)

but instead my code displays 4 hrs 43 minutes. How to convert days to hours ?

string waitTime = null;
TimeSpan whatTime;
TimeSpan getMinutes;
DateTime createTime;
double totalMinutes;

createTime = (DateTime)CreateDate;
whatTime = DateTime.Now.Subtract(createTime);

totalMinutes = whatTime.TotalMinutes;
getMinutes = TimeSpan.FromMinutes(totalMinutes);
waitTime = getMinutes.ToString(@"hh\:mm");

How to convert all the minutes to hours and minutes?

• ### Re: Converting Minutes to Hours and Minutes

#### SSpost

getMinutes = TimeSpan.FromMinutes(totalMinutes);

use TotalHours like below

```string waitTime;
TimeSpan whatTime;
TimeSpan getMinutes;
DateTime createTime;
double totalMinutes;
totalMinutes = 11803.222;
getMinutes = TimeSpan.FromMinutes(totalMinutes);
//Use Total hours here
waitTime = getMinutes.TotalHours.ToString("N2");```
Thanks,
Aje
My Blog | Dotnet Funda
• ### Re: Converting Minutes to Hours and Minutes

Considering you have a total minutes (including decimal part). why not use basic calculation?
Using sample bellow 11803.228 I get translated to 196h 43m 13s 0.680ms

```Dim MinutesInput As Decimal = 11803.228                     ' Total of Minutes
Dim HoursLeft As Decimal = MinutesInput / 60                ' Minutes to Decimal Hours
Dim Hours As Integer = Math.Truncate(HoursLeft)             ' Hours
Dim MinutesLeft As Decimal = (HoursLeft - Hours) * 60       ' Decimal Minutes Left from Hours
Dim Minutes As Integer = Math.Truncate(MinutesLeft)         ' Minutes
Dim SecondsLeft As Decimal = (MinutesLeft - Minutes) * 60   ' Decimal Seconds Left from Minutes
Dim Seconds As Integer = Math.Truncate(SecondsLeft)         ' Seconds
Dim Milliseconds As Decimal = Math.Round(SecondsLeft - Seconds, 3) ' Milliseconds roundedDim Txt As String = Hours & "h " & Minutes & "m " & Seconds & "s " & Milliseconds & "ms" ' Final result```

Same value gets translated to "8day(s) 4h 43m 13s 0.680ms" with sample bellow

```Dim MinutesInput As Decimal = 11803.228                     ' Total of Minutes
Dim DaysLeft As Decimal = MinutesInput / (24 * 60)          ' Decimal Days from total Minutes
Dim Days As Integer = Math.Truncate(DaysLeft)               ' Days
Dim HoursLeft As Decimal = (DaysLeft - Days) * 24           ' Decimal Hours left from Days
Dim Hours As Integer = Math.Truncate(HoursLeft)             ' Hours
Dim MinutesLeft As Decimal = (HoursLeft - Hours) * 60       ' Decimal Minutes Left from Hours
Dim Minutes As Integer = Math.Truncate(MinutesLeft)         ' Minutes
Dim SecondsLeft As Decimal = (MinutesLeft - Minutes) * 60   ' Decimal Seconds Left from Minutes
Dim Seconds As Integer = Math.Truncate(SecondsLeft)         ' Seconds
Dim Milliseconds As Decimal = Math.Round(SecondsLeft - Seconds, 3) ' Milliseconds rounded
Dim Txt As String = Days & "day(s) " & Hours & "h " & Minutes & "m " & Seconds & "s " & Milliseconds & "ms"```

• ### Re: Converting Minutes to Hours and Minutes

Nov 07, 2018 10:02 AM|Jenifer Jiang|LINK

Hi SSpost,

According to your description, I've made a sample and maybe you could refer to.

The key point is to use the function TimeSpan.FromMinutes().

```                var totalMinutes = 11803.228;
var time = TimeSpan.FromMinutes(totalMinutes);
var hourresult=(int)time.TotalHours;
var miniuteresult=(int)((time.TotalHours-(int)time.TotalHours)*100);
Console.WriteLine("{0}:{1}", hourresult, miniuteresult);```

The running result is:

`196:72`

Best Regards,

Jenifer

MSDN Community Support
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
• ### Re: Converting Minutes to Hours and Minutes

#### SSpost

196 hours and 72 minutes

Just in case, this is Decimal Hours not hours and minutes.
In addition to A2H and Jenifer Jiang, another sample, where you can see from where came 4h 43 minutes
Result is: 8d 4h 43m 13s 680ms

```var totalMinutes = 11803.228;
var tspan = TimeSpan.FromMinutes(totalMinutes);
Console.WriteLine(tspan.Days + "d " + tspan.Hours + "h " + tspan.Minutes + "m " + tspan.Seconds + "s " + tspan.Milliseconds + "ms");```

• ### Re: Converting Minutes to Hours and Minutes

#### SSpost

I have 11803.228 minutes and it should display 196 hours and 72 minutes

197 hours and 12 minutes, no?

Or do you mean 196.72 hours (196 hours, 43 minutes, 13 seconds and a bit)?

• ### Re: Converting Minutes to Hours and Minutes

This gives the output as 196.72 which is total hours, I want Hours : Minutes which will be 196:43, how to do that?

@A2H solution

use TotalHours like below

```string waitTime;
TimeSpan whatTime;
TimeSpan getMinutes;
DateTime createTime;
double totalMinutes;
totalMinutes = 11803.222;
getMinutes = TimeSpan.FromMinutes(totalMinutes);
//Use Total hours here
waitTime = getMinutes.TotalHours.ToString("N2");```
• ### Re: Converting Minutes to Hours and Minutes

Yes I mean 196 hours : 43 minutes

@Mikesdotnetting

#### SSpost

I have 11803.228 minutes and it should display 196 hours and 72 minutes

197 hours and 12 minutes, no?

Or do you mean 196.72 hours (196 hours, 43 minutes, 13 seconds and a bit)?

• ### Re: Converting Minutes to Hours and Minutes

@jZero

The solution below is perfect but only problem I have minutes in TimeSpan not Decimal, after I convert that

I get errors like

'Operator '/' cannot be applied to operands on type decimal and double'

once I fixed all that the below solution worked . Thanks so much :)

```Dim MinutesInput As Decimal = 11803.228                     ' Total of Minutes
Dim HoursLeft As Decimal = MinutesInput / 60                ' Minutes to Decimal Hours
Dim Hours As Integer = Math.Truncate(HoursLeft)             ' Hours
Dim MinutesLeft As Decimal = (HoursLeft - Hours) * 60       ' Decimal Minutes Left from Hours
Dim Minutes As Integer = Math.Truncate(MinutesLeft)         ' Minutes
Dim SecondsLeft As Decimal = (MinutesLeft - Minutes) * 60   ' Decimal Seconds Left from Minutes
Dim Seconds As Integer = Math.Truncate(SecondsLeft)         ' Seconds
Dim Milliseconds As Decimal = Math.Round(SecondsLeft - Seconds, 3) ' Milliseconds roundedDim Txt As String = Hours & "h " & Minutes & "m " & Seconds & "s " & Milliseconds & "ms" ' Final result```
• ### Re: Converting Minutes to Hours and Minutes

SSpost,
In you code you already converting Timespan to Double, so if using my first sample (Basic calc) just replace from Decimal to Double.

```whatTime = DateTime.Now.Subtract(createTime);
totalMinutes = whatTime.TotalMinutes;```

Or just use

```TimeSpan whatTime;
DateTime createTime;
createTime = (DateTime)CreateDate;
whatTime = DateTime.Now.Subtract(createTime);
Console.WriteLine(Math.Truncate(whatTime.TotalHours) + "h " + whatTime.Minutes + "m " + whatTime.Seconds + "s " + whatTime.Milliseconds + "ms");```