Solved

Adding Time

Posted on 2000-02-29
10
134 Views
Last Modified: 2010-05-02
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
0
Comment
Question by:dsteers
  • 4
  • 3
  • 3
10 Comments
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 2571167
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
 

Author Comment

by:dsteers
ID: 2571193
Thanks I'll Try It.
0
 

Accepted Solution

by:
d0hb0y earned 90 total points
ID: 2571414
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
 

Expert Comment

by:d0hb0y
ID: 2571419
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
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 2572450
d0hb0y,

You don't respect the input format!!!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Expert Comment

by:d0hb0y
ID: 2572931
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
 

Expert Comment

by:d0hb0y
ID: 2576831
oops, the last line of the function should read

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

Author Comment

by:dsteers
ID: 2579042
Adjusted points to 90
0
 

Author Comment

by:dsteers
ID: 2579043
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
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 2579966
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now