Solved

Adding Time

Posted on 2000-02-29
10
133 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
Comment Utility
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
Comment Utility
Thanks I'll Try It.
0
 

Accepted Solution

by:
d0hb0y earned 90 total points
Comment Utility
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
Comment Utility
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
Comment Utility
d0hb0y,

You don't respect the input format!!!
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Expert Comment

by:d0hb0y
Comment Utility
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
Comment Utility
oops, the last line of the function should read

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

Author Comment

by:dsteers
Comment Utility
Adjusted points to 90
0
 

Author Comment

by:dsteers
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

762 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

7 Experts available now in Live!

Get 1:1 Help Now