Thanks:  0
Likes:  0
Dislikes:  0

1. ## datedifference in milliseconds

I have been trying to find datedifference in milliseconds.
Example
MyTime1 = Format(Time,"hh:mm:ss:ms")
MyTime2 = Format(Time,"hh:mm:ss:ms")

How to find the difference between the two times.
Usual Datediff is not providing any string parameter for milliseconds.

2. ## Re: datedifference in milliseconds

This will give you the time of day down to the millesecond.
You can write your own diff function.

Sub Main()

Dim MsPastMidnight As Double ' Milleseconds past Midnight
Dim dblHours As Double
Dim dblMinutes As Double
Dim dblSeconds As Double
Dim TimeOfDay As Variant

MsPastMidnight = Timer()
dblHours = MsPastMidnight \ 3600
dblMinutes = (MsPastMidnight - dblHours * 3600) \ 60
dblSeconds = MsPastMidnight - dblHours * 3600 - dblMinutes * 60

MsgBox MsPastMidnight
TimeOfDay = dblHours &amp; ":" &amp; dblMinutes &amp; ":" &amp; Format(dblSeconds, "00.000")
MsgBox TimeOfDay

End Sub

Joe

3. ## Re: datedifference in milliseconds

Dear Jcd123,
Thanks for the information.
But with this i can find the time in milliseconds that much I understand. But with Format()Function i can get the time in milliseconds, my question is how to find the difference between two times which are in milliseconds ie
MyTime1 = Format(Time,"hh:mm:ss:ms")
MyTime2 = Format(Time,"hh:mm:ss:ms")
Result=DateDiff("?",MyTime1,MyTime2)
I dont know which letter should keeep in the ? place to get the timedifference in milliseconds.

else if is there any other function to find the difference betweeen the two times, kindly let me know.
else can you explain briefly the above mentioned proceduere.

4. ## Re: datedifference in milliseconds

I can't find the :ms format documented any where.
I don't think that it's valid.
When I try the following code:

MyTime1 = Format(Time, "hh:mm:ss:ms")
MsgBox MyTime1

I always get a result such as 07:37:19:1219
where the four digits of :ms are always 12
and the seconds repeated.

The only way that I know of, to get time to the millisecond
is to use the the Timer Function. From TP VBA Help:

Timer Function
Returns a Single representing the number of seconds elapsed since midnight.

Syntax
Timer

Remarks
In Microsoft Windows the Timer function returns fractional portions of a second.
On the Macintosh, timer resolution is one second.

One quirk of Timer is that if you get ss.000, the .000 is not returned.

You're going to have to write your own time format(code above),
and write your own diff function.

Joe

5. ## Re: datedifference in milliseconds

Yes My dear Jcd123,
you are right.
I did not identified the thing about ms.
Which is not giving exact milliseconds.
Good one.

My Code As follows

MsPastMidnight1 = Timer()
Sleep 1
MsPastMidnight2 = Timer()
MsPastMidnight = MsPastMidnight2 - MsPastMidnight1

dblHours = MsPastMidnight \ 3600
dblMinutes = (MsPastMidnight - dblHours * 3600) \ 60
dblSeconds = MsPastMidnight - dblHours * 3600 - dblMinutes * 60

MsgBox MsPastMidnight
TimeOfDay = dblHours &amp; ":" &amp; dblMinutes &amp; ":" &amp; Format(dblSeconds, "00:000")

MsgBox TimeOfDay

In this case atleast i should get 1 second difference but i am getting 00:003.
which is not giving right answer.
in this case whether my interpretion is wrong.

but can you please provide a function to find the difference between to times which are in milliseconds.

6. ## Re: datedifference in milliseconds

1. You miscopied my code:
its "00.000" not "00:000"

2. Don't use Sleep 1, because
the difference will always be 1.000 seconds.
See "quirk" above.

3. Replace Sleep 1 with
Pause 1234, tpPauseMilliSeconds
or any other number with non-zero milliseconds

Joe

7. ## Re: datedifference in milliseconds

I have done as you said ie as follows

MsPastMidnight1 = Timer()
Pause 1, tpPauseMilliSeconds
MsPastMidnight2 = Timer()
MsPastMidnight = MsPastMidnight2 - MsPastMidnight1

dblHours = MsPastMidnight \ 3600
dblMinutes = (MsPastMidnight - dblHours * 3600) \ 60
dblSeconds = MsPastMidnight - dblHours * 3600 - dblMinutes * 60

MsgBox MsPastMidnight
TimeOfDay = dblHours &amp; ":" &amp; dblMinutes &amp; ":" &amp; Format(dblSeconds, "00.000")

MsgBox TimeOfDay

I Expected TimeofDay value is 1 millisecond but it is returning 0:0:02.233 is this right or wrong?

Can you please check it again and provide me a sound solution for the problem.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Search Engine Optimisation provided by DragonByte SEO v2.0.36 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.