-
I have three variables
TotalHours As Single
TotalMin As Single
TotalTime As Single
TotalHours contains 40
TotalMin contains 70
I want to display this as 41:20
That is 41:20 Minutes. I need 60 of those minutes to roll over into a full hour.
How would I do this?
Please Help Me.
Ken Devorak
-
Code:
Private Sub Command1_Click()
Dim TotalHours As Single
Dim TotalMin As Single
Dim TotalTime As Single
TotalHours = Text1.Text
TotalMin = Text2.Text
MsgBox TotalHours + Int(TotalMin / 60) & ":" & TotalMin Mod 60
End Sub
Int function returns the value of whatever number(in this case, 70/60) as a whole and unrounded number. Mod function returns the remainder of the number on the left divided by the number on the right
cheers
Andrew
-
You mentioned earlier that you're learning about working with time. I see now that you've considered a strategy using the date/datetime datatype, instead of running the gamut of string/char and integer-based manipulations. FWIW, a date/datetime datatype is essentially a double where the whole number represents days and the fractional part the hours, minutes, seconds, etc.; and since it doesn't appear you’re worried about sub-second precision and/or tracking whole days, a single datatype is a good choice. Now you can do your math up front and then simply format as desired. For example:
Sub Single_Mind()
Dim sgl_tm_val As Single, i As Integer
sgl_tm_val = Adj_Tm(sgl_tm_val, 0, 50, 15, 0) ' add 50 hours, 15 minutes
sgl_tm_val = Adj_Tm(sgl_tm_val, 0, -10, 0, 0) ' for demo sake, subtract 10 hours
For i = 0 To 3
MsgBox Get_Tm(sgl_tm_val, i)
Next i
End Sub
Function Adj_Tm(tm_val As Single, dd As Integer, _
hh As Integer, mm As Integer, ss As Integer) As Single
Adj_Tm = tm_val + dd + (hh / 24) + (mm / 24 / 60) + (ss / 24 / 60 / 60)
End Function
Function Get_Tm(tm_val As Single, tm_type As Integer) As String
Select Case tm_type
Case 0: Get_Tm = tm_val & " total days"
Case 1: Get_Tm = whole_u("d", tm_val) & " day, " & mod_u("d", tm_val) & " hours"
Case 2: Get_Tm = whole_u("h", tm_val) & " hours, " & mod_u("h", tm_val) & " minutes"
Case Else: Get_Tm = "Get the idea?"
End Select
End Function
Function whole_u(u_type As String, tm_val As Single) As Integer
Select Case u_type
Case "d": whole_u = tm_val * 24 \ 24
Case "h": whole_u = tm_val * 24 * 60 \ 60
Case "m": whole_u = tm_val * 24 * 60 * 60 \ 60
Case Else: whole_u = 0
End Select
End Function
Function mod_u(u_type As String, tm_val As Single) As Integer
Select Case u_type
Case "d": mod_u = (tm_val * 24) Mod 24
Case "h": mod_u = (tm_val * 24 * 60) Mod 60
Case "m": mod_u = (tm_val * 24 * 60 * 60) Mod 60
Case Else: mod_u = 0
End Select
End Function