Solved

Posted on 2000-02-29
138 Views
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
~~~~~~~~~

Thanks
0
Question by:dsteers
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4
• 3
• 3

LVL 70

Expert Comment

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

ID: 2571193
Thanks I'll Try It.
0

Accepted Solution

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

'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)

' 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

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 70

Expert Comment

ID: 2572450
d0hb0y,

You don't respect the input format!!!
0

Expert Comment

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

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

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

Author Comment

ID: 2579042
0

Author Comment

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 70

Expert Comment

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

List1.Clear
End Sub
0

## Featured Post

Question has a verified solution.

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

### Suggested Solutions

Put text in a picture ASP.NET C# 2 73
Copy a row 12 76
How to read File Date Created using VB6 8 71
VB6 - Cannot type when doing an extract from MSHFlexgrid to Excel 6 48
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
###### Suggested Courses
Course of the Month2 days, 12 hours left to enroll