calculate Time Difference in milisecond

tapai_rhino

Member
Joined
Jan 13, 2006
Messages
16
Programming Experience
Beginner
Hi Everybody..

I have 2 time in mm:ss.nn format.. so my question is how can i get the difference between the two time in the format mm:ss.nn..

eg:

firstTime = 11:10.5
secTime = 12:55.9

timediff = 01:45.4


thanks
 
Here is one solution :

VB.NET:
        Dim FirstTime As DateTime = TimeSerial(11, 10, 5)
        Dim SecTime As DateTime = TimeSerial(12, 55, 9)
        Dim TimeDiff As TimeSpan = SecTime.Subtract(FirstTime)
        Console.WriteLine(TimeDiff)

If you only want to use this to evaluate your code, you can use the the StopWatch class.
 
Calculate time difference in Basic

Will this code calculate the difference between two stamps of format like this:-

TIME1 = 2006-12-01 09:12:45

TIME2 = 2006-12-01 11:48:03

In other words, how can I calculate the difference between the two and then test if the time difference is greater than 30Mins.

Any help much appreciated.

Thanks
 
VB.NET:
Dim dt1 As Date = #12/01/2006 9:12:45#
Dim dt2 As Date = #12/01/2006 11:48:03#
Dim diff As TimeSpan = dt2 - dt2

If diff.TotalMinutes > 30.0 Then
    'Time difference greater than thirty minutes.
End If
Note that you can use standard arithmetic operators on Dates and TimeSpans in VB 2005 but in previous versions you must use the Subtract method.
 
Calculate time difference

Many thanks for this.

My two times will always be different though.

I will read a screen and capture the date and time from a user time stamp, this could be dt1

I will then need to get the current system time, assume this could be dt2. What I then want to do is check if 30mins have elapsed since the user time stamp, ie test if dt1 is at least 30mins older than the current time.

So, would something like this work?

Dim dt1 As DateTime
Dim dt2 As DateTime
Dim diff As TimeSpan = dt2 - dt1

Sub timetest()

dt1 = Sess0.Screen.Getstring (01 ,08)

dt2 = DateTime

REM I am not sure how to get the current system time?

If diff.TotalMinutes > 30.0 Then
'Time difference greater than thirty minutes.

call myprogram

End If

End Sub


Thanks in advance.
 
You get the current time using Date.Now.

It doesn't matter where your date/time values come from. I just used literal values for illustrative purposes. Never assume that a code example is anything more than an example.

Date objects are Date objects and once you have two of them from whatever source then you can treat them in exactly the same way.
 
calculate time difference

Many thanks.

All sorted now.

I am actually using Attachmate Extra basic, so its a little tricky, but I have something working now.

Regards
 
Back
Top