# How to Compute the time duration pls help me!

vba
• 14 years ago

i am a bigginer in vb so i have many things i dont know how to do yet i have a small program that stores the time you started and the time you stoped what i dont know is how to compute for there duration i tried to make it as a string as an intiger but i always get the answer zero please help me very urgent please this is my last hope to pass on my project please my e-mail is [email protected] tell me what to do please......................................

• 14 years ago

Niko,

You will have to give a bit more information, what Office Application are you using?  how are you getting the start/stop time?

One option is to get the current date/time when you start a process:
Dim starttime as Date
Dim endtime as Date
Dim myinterval as Long
starttime = Now
endtime = Now

You can then compare the start and end times using the DateDiff function, this example will give the difference in seconds:
myinterval = DateDiff("s", starttime, endtime)
Check this out:
http://freespace.virgin.net/s.cowan/vbhowto/how_to/mathematical/datediff.html

Another option would be to add a Timer Control to your form, select the Timer icon from the Toolbox (it is the one that looks like a stopwatch) and drag it onto your form.  Change its "Interval" property to 1000, this will count in seconds.  You can then get the start and end times in a similar manner:
Dim starttime As Double
Dim endtime As Double
Dim myinterval As Double
starttime = Timer
endtime = Timer
myinterval = endtime - starttime
'This will give you the answer in seconds, you could divide it by sixty to give minutes:
myinterval = (endtime - starttime) / 60

• 14 years ago

here is some more iformation about what im trying to solve sir lbltime.caption is where the current time is viewed then i have 3 more label's that is lblin.caption where you store the log in of a user then lblout.caption and this is the log out and the lbldur.caption where i want the computation of how much time is consumed im using visial basic to program a command button called start gets the log in and out button calls the log out and button duration for the computation here is the syntax of the program

private sub timer()

lbltime.caption = time()

______________________________

private sub command_cmdstart()

lblin.caption = lbltime.caption

______________________________

private sub command_cmdout()

lblout.caption = lbltime.caption

______________________________
private sub command_duration()

time1 = val(lblin.caption)

time2 = val(lblout.caption)

lbldur.caption = val(time2 - time1)

i tried to make time1 and time2 as an integer and as a long but the answer always returns zero please help me and thank you very much

• 14 years ago

Hi Niko,

Your problem is the fact that you are declaring time1 and time2 as numbers, you need to treat them as dates.  It doesn't matter that they are just times, the Date data type is used for dates or times.  Also, you are using val(lblout.caption), again, this wont work - you have to treat them as dates, use CDate instead of Val.  The following code will calculate the difference and store it in the variable called dur, because you are treating the variables as dates you will get the output formatted as hh:mm:ss:

Dim time1 As Date
Dim time2 As Date
Dim dur As Date

lbltime.Caption = Time()
End Sub

Private Sub Command1_Click()
lblin.Caption = CDate(lbltime.Caption)
End Sub

Private Sub command2_Click()
lblout.Caption = Time()
End Sub

Private Sub command3_click()
time1 = CDate(lblin.Caption)
time2 = CDate(lblout.Caption)
dur = time2 - time1
lbldur.Caption = dur
End Sub

• 14 years ago
thank you very much to your help my god give you more blessing than ever you saved my life by answering my questions again thank you and more power to you so till the next question thank you very much

### Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

### Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Walking on water and developing software from a specification are easy if both are frozen.”