Adding Time

Can anyone please tell me how to add hours and minutes to each other.
Seems like a simple thing to do and maybe it is (well not for me).
I wish to add (for example):
1 h 56 m
2 h 24 m
5 h 43 m
~~~~~~~~~
Answer

Thanks
dsteersAsked:
Who is Participating?
 
d0hb0yConnect With a Mentor Commented:
Wow, emoreau recommended the HARD way to do it. If you are running Visual Basic 6.0, I suggect trying the following code to see if it helps you.

===[ Cut Here ]==============

Private Sub Command1_Click()

Dim temp As Date

' add these times together
'1 h 56 m
'2 h 24 m
'5 h 43 m

temp = CDate("01:56:00")
temp = temp + CDate("02:24:00")
temp = temp + CDate("05:43:00")

' here is the total time
Debug.Print Duration_Text$(temp)

' additional, lets add 20 more hours so it
' rolls into the next day

temp = temp + CDate("10:00:00")
temp = temp + CDate("10:00:00")

' what is the total now?
Debug.Print Duration_Text$(temp)

End Sub


Function Duration_Text$(ByVal Duration@)
Dim hrs%, mins%, tmp$

dtime$ = Format(Duration@ - Int(Duration@), "HH:MM")
hrs% = Val(Left$(dtime$, 2))
mins% = Val(Right$(dtime$, 2))

If Int(Duration@) > 0 Then tmp$ = tmp$ & Int(Duration@) & " Days "
If hrs% > 0 Then tmp$ = tmp$ & hrs% & " Hours "
If mins% > 0 Then tmp$ = tmp$ & mins% & " Minutes "

Duration_Text$ = Trim$(tmp$)

End Function




0
 
Éric MoreauSenior .Net ConsultantCommented:
You seem to work hard with date and time for now!!!

I have a solution for you:

Private Sub Command1_Click()
Dim arrX As Variant
Dim lngHours As Long
Dim lngMinutes As Long
Dim X As Variant
Dim X2 As Variant

    arrX = Array("1 h 56 m", "2 h 24 m", "5 h 43 m")

    For Each X In arrX
        X = Replace(X, "m", "")
        X = Replace(X, " ", "")
        X2 = Split(X, "h")
        lngMinutes = lngMinutes + (Val(X2(0)) * 60) + Val(X2(1))
    Next
   
    'Extract Hours
    If lngMinutes > 60 Then
        lngHours = Int(lngMinutes / 60)
        lngMinutes = lngMinutes - (lngHours * 60)
    End If
     
    MsgBox "Total : " & _
           lngHours & " hours and " & _
           lngMinutes & " minutes."
End Sub
0
 
dsteersAuthor Commented:
Thanks I'll Try It.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
d0hb0yCommented:
I forgot to include the output to the to my routine, here it is (from the debug window)

10 Hours 3 Minutes
1 Days 6 Hours 3 Minutes

0
 
Éric MoreauSenior .Net ConsultantCommented:
d0hb0y,

You don't respect the input format!!!
0
 
d0hb0yCommented:
OK, this function will allow the user to use the provided input format

function FixTime$(oldtime$)

' 1 h 56 m
' 2 h 24 m
' 5 h 43 m

  h% = instr(OldTime$,"h")
  m% = instr(OldTime$,"m")
 
 Hours%=val(left$(oldtime$,h%-1))
 Minutes% = val(mid$(oldtime$,h%+1, m%-h%) )

HourText$ = format$(hours%,"##")+":"+format$(minutes%,"##")

end function

then the user can do the following:
temp=temp+cdate(fixtime("4 h 22 m"))

Although I am guessing that the input format is not the -real- issue; but the technique of adding times probably is.  

 
0
 
d0hb0yCommented:
oops, the last line of the function should read

FixTime$ = format$(hours%,"##")+":"+format$(minutes%,"##")
0
 
dsteersAuthor Commented:
Adjusted points to 90
0
 
dsteersAuthor Commented:
Ok sorry Guys,
All I was looking for was :
you would have 2 textboxes ,One for Hour and One for Minutes.
Entered just as numbers ,Sorry :(
Once the numbers are entered it would add them to a listbox (after pressing a command button).
This would continue until another command button was pressed indicating that the user has finished.
It then adds up all the hours and minutes in the listbox and displays the total.

The hours and minutes in the Listbox would be displayed as :
1 h 15 m  or
1 hour/s  15 min/s

Sorry once again for being so misleading,and not communicating properly.

P.S I've increased the points to 90 because I realise the trouble caused.

Thanks
Dave Steers

0
 
Éric MoreauSenior .Net ConsultantCommented:
You can try this:

Private Sub Command1_Click()
    List1.AddItem Text1.Text & ":" & Text2.Text
End Sub

Private Sub Command2_Click()
Dim intI As Integer
Dim lngHours As Long
Dim lngMinutes As Long
Dim X2 As Variant

    For intI = 0 To List1.ListCount - 1
        X2 = Split(List1.List(intI), ":")
        lngMinutes = lngMinutes + (Val(X2(0)) * 60) + Val(X2(1))
    Next
   
    'Extract Hours
    If lngMinutes > 60 Then
        lngHours = Int(lngMinutes / 60)
        lngMinutes = lngMinutes - (lngHours * 60)
    End If

    MsgBox "Total : " & _
           lngHours & " hours and " & _
           lngMinutes & " minutes."
End Sub

Private Sub Form_Load()
    List1.Clear
End Sub
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.