Answered by:
Converting Minutes to Hours and Minutes

Question
-
User-13689440 posted
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?
Tuesday, November 6, 2018 10:13 PM
Answers
-
User-943250815 posted
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.680msDim 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 rounded
Dim Txt As String = Hours & "h " & Minutes & "m " & Seconds & "s " & Milliseconds & "ms" ' Final resultSame 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"
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, November 7, 2018 12:38 AM
All replies
-
User2103319870 posted
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");
Wednesday, November 7, 2018 12:08 AM -
User-943250815 posted
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.680msDim 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 rounded
Dim Txt As String = Hours & "h " & Minutes & "m " & Seconds & "s " & Milliseconds & "ms" ' Final resultSame 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"
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, November 7, 2018 12:38 AM -
User839733648 posted
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
Wednesday, November 7, 2018 10:02 AM -
User-943250815 posted
SSpost
196 hours and 72 minutesJust 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 680msvar 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");
Wednesday, November 7, 2018 11:08 AM -
User-821857111 posted
SSpost
I have 11803.228 minutes and it should display 196 hours and 72 minutes197 hours and 12 minutes, no?
Or do you mean 196.72 hours (196 hours, 43 minutes, 13 seconds and a bit)?
Wednesday, November 7, 2018 4:55 PM -
User-13689440 posted
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");
Thursday, November 8, 2018 7:10 PM -
User-13689440 posted
Yes I mean 196 hours : 43 minutes
@Mikesdotnetting
SSpost
I have 11803.228 minutes and it should display 196 hours and 72 minutes197 hours and 12 minutes, no?
Or do you mean 196.72 hours (196 hours, 43 minutes, 13 seconds and a bit)?
Thursday, November 8, 2018 7:14 PM -
User-13689440 posted
@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 rounded
Dim Txt As String = Hours & "h " & Minutes & "m " & Seconds & "s " & Milliseconds & "ms" ' Final resultThursday, November 8, 2018 7:26 PM -
User-943250815 posted
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");
Friday, November 9, 2018 11:24 AM