Turn total minutes to HH:MM

I have a variable that calculates total minutes when I display this I need the number to be formatted as HH:MM instead of only minutes
huerita37Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
You could do something like:
Option Explicit
 
Private Sub CommandButton1_Click()
    Dim totalMinutes As Integer
    Dim formatted As String
    
    totalMinutes = 93
    formatted = FormatMinutes(totalMinutes)
    MsgBox formatted
End Sub
 
Private Function FormatMinutes(ByVal totalMinutes As Integer) As String
    FormatMinutes = Format(DateAdd("n", totalMinutes, Date), "Short Time")
End Function

Open in new window

0
huerita37Author Commented:
I tried using this but I am not getting the correct HH:MM.  For example,

my totalMinutes = 197236 and the function is returning 23:16  Obviously this is incorrect.  I did change the Interger type to Long because it was failing.  Could this be the problem?  Do I need to set it to something else?
0
xPert_UmerCommented:
mod(hours,23) will give you days, (hours - mod(hours,23)*24) will give you the correct hours.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

gnoonCommented:
>I did change the Interger type to Long because it was failing
Maximum value for Integer is 32767

However, is this you want

hhmm = min2HHMM(197236) '--> output: 3287:16
Function min2HHMM(ByVal minute As Long) As String
	Dim h As Integer
	Dim m As Integer
	Dim hh As String
	Dim mm As String
	h = Fix(minute/60)
	m = minute Mod 60
	hh = CStr(h)
	mm = CStr(m)
	If h<10 Then hh="0" & h End If
	If m<10 Then mm="0" & m End If
	min2HHMM = hh & ":" & mm
End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike TomlinsonMiddle School Assistant TeacherCommented:
Sorry....my approach will only work for values less than 24 hours.  For larger intervals you will need to calculate it manually as the others have shown...   =\
0
dentabCommented:
Emulating Idle_Minds code...

I dont see why this would be retricted to 24 hours:
Function flr(byval thisValue as Double) As Double
  If CLng(thisValue)>thisValue Then thisValue = thisValue - 1
  thisValue = Clng(thisValue)
End Function
 
Function min2HHMM(ByVal minute As Double) As String
  Dim h As Long
  Dim hh As String, mm As String
  h = flr(minute/60)
  minute = minute - (h * 60) 'Wrap expression with CLNG for whole minutes - CLng(minute - (h * 60))
  
  hh = CStr(h)
  mm = CStr(minute)
 
  While Instr(mm & ".", ".") < 2
    mm = "0" & mm
  Wend
 
  While Len(hh)<2
    hh = "0" & hh
  Wend
 
  min2HHMM = hh & ":" & mm
End Function

Open in new window

0
dentabCommented:
woops 2 mistakes there...

fixed below

Function flr(ByVal thisValue As Double) As Double
  If CLng(thisValue) > thisValue Then thisValue = thisValue - 1
  flr = CLng(thisValue)
End Function
 
Function min2HHMM(ByVal minute As Double) As String
  Dim h As Long
  Dim hh As String, mm As String
  
  h = flr(minute / 60)
  minute = CLng(minute - (h * 60))60))
  ' minute = minute - (h * 60))60) 'Use this version instead to include fractions of minutes
  
  hh = CStr(h)
  mm = CStr(minute)
 
  While InStr(mm & ".", ".") < 3
    mm = "0" & mm
  Wend
 
  While Len(hh) < 2
    hh = "0" & hh
  Wend
 
  min2HHMM = hh & ":" & mm
End Function

Open in new window

0
dentabCommented:
omg, did it again

last one...
Function flr(ByVal thisValue As Double) As Double
  If CLng(thisValue) > thisValue Then thisValue = thisValue - 1
  flr = CLng(thisValue)
End Function
 
Function min2HHMM(ByVal minute As Double) As String
  Dim h As Long
  Dim hh As String, mm As String
  
  h = flr(minute / 60)
  minute = CLng(minute - (h * 60))
  
  hh = CStr(h)
  mm = CStr(minute)
 
  While InStr(mm & ".", ".") < 3
    mm = "0" & mm
  Wend
 
  While Len(hh) < 2
    hh = "0" & hh
  Wend
 
  min2HHMM = hh & ":" & mm
End Function

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.