Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how can calculate hours in vb 6

Status
Not open for further replies.

darkdido

Programmer
Nov 23, 2005
8
SD
how can calculate time in vb 6
like..(30:30:00+20:15:00)=50:45:00

30 hours and 30 minutes + 20 hours and 15 minutes
 
There is probably a more elegant way to accomplish this, but....

Code:
    Dim Time1 As String
    Dim Time2 As String
    Dim Hours As Integer
    Dim Minutes As Integer
    
    Time1 = "30:30"
    Time2 = "20:45"
    
    Hours = CInt(Split(Time1, ":")(0)) + CInt(Split(Time2, ":")(0))
    Minutes = CInt(Split(Time1, ":")(1)) + CInt(Split(Time2, ":")(1))
    
    Hours = Hours + CInt(Minutes / 60 - 0.5)
    Minutes = Minutes Mod 60
    
    Debug.Print Hours & ":" & Minutes

When adding hours and minutes, you have to be careful about the minutes causing a 'rollover' for the hours. Ex. 1 hour 45 minutes + 1 Hour 30 minutes is not 2 hours 75 minutes, but really 3 hours and 15 minutes. The code I present here should accomodate this.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
DateAdd won't work, since say 30 hours isn't a valid date and you'll get a type mismatch.
 
BobRodes,

I tried this one:

MsgBox Now & "..... " & DateAdd("h", 30, Now)

And it works just fine....

Any thoughts?

vladk
 
<Any thoughts?

Um...I'm wrong?

I got a type mismatch using CDate in my formula, it seems. Hey, I been sick....

But since we're on this, vladk, how do we format your result so that it conforms to the OP's format, rather than showing what time it would be if you added one datetime to another? That's what I was struggling with.

Bob



 
BobRodes,

I apologize, what is OP's format?

vladk
 
Thank you strongm.

I hope the text below taken from help file is what OP asked for.

The TimeSerial function returns a time for 15 minutes before (-15) six hours before noon (12 - 6), or 5:45:00 A.M.

TimeSerial(12 - 6, -15, 0)

When any argument exceeds the normal range for that argument, it increments to the next larger unit as appropriate. For example, if you specify 75 minutes, it is evaluated as one hour and 15 minutes. If any single argument is outside the range -32,768 to 32,767, an error occurs. If the time specified by the three arguments causes the date to fall outside the acceptable range of dates, an error occurs.

vladk

 
Vladk,

Now put this in the context of the original poster's question.

30 Hours and 30 Minutes
+ 20 Hours and 15 Minutes
= 50 Hours and 45 Minutes

TimeSerial returns a Date data type. Since 50 hours is greater than a day, time serial returns a date portion with the date incremented (by 2 because 50 is greater than 2 days but less than 3).

So, using the TimeSerial method, you'll have to get the number of days added, then add the number of hours, and finally concantenate a ":" and the number of minutes.

If you assume that darkdido started with strings, then this code would also work.

Code:
Dim dteTemp As Date
Dim Time1 As String
Dim Time2 As String

Time1 = "30:30"
Time2 = "20:15"

dteTemp = TimeSerial(CInt(Split(Time1, ":")(0)) + CInt(Split(Time2, ":")(0)), CInt(Split(Time1, ":")(1)) + CInt(Split(Time2, ":")(1)), 0)

MsgBox DateDiff("D", CDate(0), dteTemp) * 24 + Hour(dteTemp) & ":" & Minute(dteTemp)

Personally, I don't see how that is much better than my original solution.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
George

This is my version. I didn't test it.I assumed that you let any number of hours while minutes and seconds are between 0 and 60. Even if results are not exactly right, I think you will get my idea: just play with remainders and whole parts.


Dim dtmCurrentTime As Date
Dim strNewTime As String
Dim Hr As Integer
Dim HrsMore As Integer
Dim Mn As Integer
Dim MnMore As Integer
Dim Sc As Integer
Dim ScMore As Integer

dtmCurrentTime = Time

Sc = Second(dtmCurrentTime) + 120
MnMore = Sc \ 60
Sc = Sc Mod 60
Mn = Minute(dtmCurrentTime) + 360 + MnMore
HrsMore = Mn \ 60
Mn = Mn Mod 60
Hr = Hour(dtmCurrentTime) + 20 + HrsMore

MsgBox CStr(Hr) & ":" & Format(Mn, "00") & ":" & Format(Sc, "00")

 
George,

You don't need split. Use Hour, Minute, and Second
 
vladk,

That won't work because the OP started with "30:30:00".

Just for fun, try...

msgbox Hour("30:30:00")



-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
George,
I gave an example. It works, just use 30 in straight way instead of actual extracting number of hours from the current time.

Regarding your last example:
I think, Hour applies to the date data type, not to a string that cannot even be converted to the date type.

vladk
 
George

I looked at your example. It is very good code...
 
Here's a piece of code I wrote to do something similar, although it's for calculating the difference between two HHMMSS formatted time values, rather than the sum. The code assumes 6 txtNum textboxes in an array, with 0-2 being subtrahend hours, minutes, and seconds, and 3-5 being those of the minuend.
Code:
Dim HH As Integer
Dim MM As Integer
Dim SS As Integer
Dim cVal As Integer
HH = CInt(txtNum(3).Text) - CInt(txtNum(0).Text)
MM = CInt(txtNum(4).Text) - CInt(txtNum(1).Text)
SS = CInt(txtNum(5).Text) - CInt(txtNum(2).Text)
If SS < 0 Then
    SS = SS + 60
    MM = MM - 1
End If
If MM < 0 Then
    MM = MM + 60
    HH = HH - 1
End If
If HH < 0 Then
    lblResult.Caption = ""
    MsgBox "From must be a lower value than to"
Else
    lblResult.Caption = IIf(HH > 0, Format(CStr(HH), "00") & ":", "") & Format(CStr(MM), "00") & ":" & Format(CStr(SS), "00")
End If
 
BobRodes,

It seems that all of us answered the OP's question. Each in the different manner. Where is OP?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top