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 TouchToneTommy 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 time long 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

i use this


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


but if the number is long in run i have error " over flow "

like..(40644:30:00+20:15:00)=406024:45:00

40644 hours and 30 minutes + 20 hours and 15 minutes

 
whay can't you convert both values to Date variables, add them, and reconvert the sum to a string
 
Try doing

Dim Hours As Long

see if that fixes your problem.
 
When I add time, I usually convert everything to seconds, do the math, then convert it back to hours and minutes.

 
This thread (thread222-1158142) may be helpful.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Guess not, since he's the op there as well, and never told us whether all the work we put in was any help at all. Apparently, it wasn't, since he's asking the same question again......

Bob
 
Bob,

I noticed that he was the same poster of the other thread. I guess I forgot to add the <Sarcasm></Sarcasm> tags to my previous post.

-George

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

My apologies. I guess I didn't read your post well enough. I now see the difference between the 2. Arguably, the posts are EXTREMELY similar, therefore you should have posted a followup in the original instead of creating a new post.

Anyway, here's the updated code to allow for longs...

Code:
Dim Time1 As String
Dim Time2 As String
Dim Hours As [red]Long[/red]
Dim Minutes As [red]Long[/red]

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

Hours = [red]CLng[/red](Split(Time1, ":")(0)) + [red]CLng[/red](Split(Time2, ":")(0))
Minutes = [red]CLng[/red](Split(Time1, ":")(1)) + [red]CLng[/red](Split(Time2, ":")(1))

Hours = Hours + [red]CLng[/red](Minutes / 60 - 0.5)
Minutes = Minutes Mod 60

Debug.Print Hours & ":" & Minutes

Again, my apologies for not reading your question well enough. To understand more about effective forum participation, I suggest you read FAQ222-2244.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top