time function

mirzao

Active member
Joined
Nov 10, 2005
Messages
44
Location
Malaysia
Programming Experience
Beginner
Hi,
i have no idea why sometimes the function given me the wrong answer. When i put a msgbox at the first line of the function, it outputs 00:00:54 but the correct answer should be 00:05:04 and sometimes 00:00:00 becomes 00:00:10. Plz help. Thanks


sub loop
sec=(sumidle(i)-1)*38
timecalc()
t=format(timecalc(),"00:00:00")
end sub
Private
Function timeCalc() As Integer

MsgBox( min & sec)
If sec >= 60 And sec < 3600 Then

min = sec \ 60
sec = sec
Mod 60
ElseIf sec >= 3600 Then

hr = sec \ 3600
min = sec
Mod 3600
If min >= 60 And min < 3600 Then

min = min \ 60
sec = min
Mod 60
End If

End If

timeCalc = hr& min & sec
End Function

 
The .net Framework has functions like add or substract on Date Variables that give you a timespan on which you can use properties like milliseconds, seconds, minutes, hours, days.

If you just need to know the elpapsed time for a portion of your code you can use the new Stopwatch object in net 2.0

If you want to implement you own code you can try this.

VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] TimeCalc([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] Seconds [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2]) [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String
 
 

[/COLOR][/SIZE][INDENT][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Days, Hours, Minutes [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2]Days = Seconds \ 86400
Seconds = Seconds [/SIZE][SIZE=2][COLOR=#0000ff]Mod[/COLOR][/SIZE][SIZE=2] 86400
Hours = Seconds \ 3600
Seconds = Seconds [/SIZE][SIZE=2][COLOR=#0000ff]Mod[/COLOR][/SIZE][SIZE=2] 3600
Minutes = Seconds \ 60
Seconds = Seconds [/SIZE][SIZE=2][COLOR=#0000ff]Mod[/COLOR][/SIZE][SIZE=2] 60
[/SIZE][SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2].Format([/SIZE][SIZE=2][COLOR=#800000]"{0:#0} d : {1:00} h : {2:00} m : {3:00} s"[/COLOR][/SIZE][SIZE=2], Days, Hours, Minutes, Seconds)
[/SIZE]
 

[/INDENT][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function
[/COLOR][/SIZE]

Note that if you were waiting for 05:54 to show in your message box, it means that min doesn't have the 5 value you were expecting and that your error is coming from another portion of your code.
 
Hi,
Thanks for your reply. The last line return an error saying that "Index (zero based) must be greater than or equal to zero and less than the size of the argument list." ,perhaps because I'm using vb.net. How to modify this line actually?
 
Hi Mirzao,

I am also using vb.net, I have tried both 2003 and 2005 ; this functions works fine, you will only have an overflow exception if you try a parameter that is not an integer (from -2,147,483,648 through 2,147,483,647).

There is no variable in this function working with an index and I don't understand why you have an index error.

Have you tried this function on a new form with one textbox to input values and another one to display the result? If it works that way than the bug is somewhere else in your program.
 
VB.NET:
Dim time As TimeSpan = TimeSpan.FromSeconds(numSeconds)

'Allow for the fact that the period may span more than 24 hours.
MessageBox.Show(String.Format("{0}:{1:d2}:{2:d2}", Math.Floor(time.TotalHours), time.Minutes, time.Seconds))
 
Another Option

One thing that I have started to use but may be new to the 2.0 framework is the stopwatch.

Just try

Dim sWatch as new stopwatch()
sWatch.start()
'code here
for x as integer = 1 to 10
runProgram()
next
sWatch.stop()

msgbox(cstr(sWatch.elapsedMilliseconds))

this will display the time ellapsed from start to stop. This may be a bit off but try to see what the stopwatch has to offer I have used it for testing the speed of a program and used to see if I have sped up the code by making changes
 
Back
Top