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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Subtract Milliseconds

Status
Not open for further replies.

ralphtrent

Programmer
Jun 2, 2003
958
US
Does anyone know of a way to do a datediff like function for times with milliseconds?
 
I don't think VBScript by it self provides millisecond time resolution at all. There may be a COM object that you could use, but I don't know of one. You could write your own in C#, C++, VB, etc.

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
How would I do it in VB. I have VB knowledge, but how would I go about doing it?
 
Search the VB forum for 'Creating COM Objects' or ask in the vb forum.

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
I know how to create a com object I mean, how would I subtract the MS in vb.
 
In that case, search the vb forum for 'millisecond'. You will find several threads dealing with millisecond resolution timing. However, windows is notoriously bad at millisecond resolution so good luck.

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
>I figured it out, thanks

Given that there are no intrinsic time functioins in VBA or VBA with millisecond resolutin, could you share your solution with us?
 
How about the timer method/object? It should be able to handle accuracy to about 16 milliseconds (i can't remember if the clock accuracy is based on CPU or OS, just know that 16 is about the best I can do on my system)

Code:
a = timer
'do stuff
'do more stuff
objShell.Popup "Stuff took " & timer - a " seconds!"

Gets a decimal value for seconds to3 or 4 digits if I remember corectly. A lot easier than pulling in a COM object or (the way I was doing it) tying into something like a Python script :p

-T

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 
Timer() returns the time since midnight as a Double value. It's resolution is good to about 3 decimal places, and on faster systems may get you close to millisecond resolution.

So just use [tt]1000.0 * Timer()[/tt] to get a usable approximation of milliseconds since midnight.
 
I needed to do MilliSeconds between two variables. So timer would not work for this.
 
What do the variable hold?

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
they hold time stamps in the this format
12:50:15.096
You can not do a cdate("12:50:15.096"). It will not work.
 
No, but you could parse the string to get two valid times, get the second difference then calculate the millisecond difference yourself.

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
>It's resolution is good to about 3 decimal places, and on faster systems may get you close to millisecond resolution

Sadly not. The Timer is based of the system clock, The system clock only ticks approx every 55 milliseconds on a WW9x/Me box, and approx every 10 milliseconds on Nt4/W2000. XP systems tend to tick either every 10ms or every 15ms (don't know why)
 
Excellent point. I should have bothered to do more than take 30 samples and eyeball them.
 
Here is code to get a millisecond difference between two string time stamps with the form hh:mm:ss.xxx. The function will only work if the timestamps are passed oldest first(if I get time I may make a corrected version to be independant of this restriction). Code up some more test cases and let me know of any bugs.
Code:
Option Explicit
Dim strTime1, strTime2, strMilliDiff

strTime1 = "12:50:15.096"
strTime2 = "12:50:15.200"
strMilliDiff = GetMilliDiff(strTime1, strTime2)
TestMilliDiff strMilliDiff, "104"

strTime1 = "12:50:15.096"
strTime2 = "12:50:16.296"
strMilliDiff = GetMilliDiff(strTime1, strTime2)
TestMilliDiff strMilliDiff, "1200"

strTime1 = "12:50:16.296"
strTime2 = "12:50:15.096"
strMilliDiff = GetMilliDiff(strTime1, strTime2)
TestMilliDiff strMilliDiff, "1200"

strTime1 = "12:50:15.096"
strTime2 = "12:50:16.006"
strMilliDiff = GetMilliDiff(strTime1, strTime2)
TestMilliDiff strMilliDiff, "910"


Sub TestMilliDiff (strToTest, strShouldBe)
	If strToTest = strShouldBe Then
		WScript.Echo "Test passed " & strToTest & " = " & strShouldBe
	Else
		WScript.Echo "Test FAILED " & strToTest & " <> " & strShouldBe
	End If
End Sub

Function GetMilliDiff(strT1, strT2)
	Dim time1, time2, milli1, milli2, arr1, arr2, timediff, millidiff
	
	arr1 = Split(strT1, ".")
	arr2 = Split(strT2, ".")
	If UBound(arr1) > 1 Or UBound(arr2) > 1 Then
		'Invalid time submitted
		GetMilliDiff = Null
		Exit Function
	End If
	If UBound(arr1) = 1 Then
		time1 = arr1(0)
		milli1 = arr1(1)
	Else
		Time1 = arr1(0)
		milli1 = "0"
	End If
	If UBound(arr2) = 1 Then
		time2 = arr2(0)
		milli2 = arr2(1)
	Else
		time2 = arr2(0)
		milli2 = "0"
	End If
	If CDate(time2) < CDate(time1) Then
		'Submitted in wrong order
		GetMilliDiff = Null
		Exit Function
	End If
	
	'First, get the second difference from the times and convert to milli
	timediff = DateDiff("s", time1, time2) * 1000
	
	'Now there will be two possibilities for the milli diff
	If milli1 <= milli2 Then
		millidiff = CInt(milli2) - CInt(milli1)
	Else
		'Get diff from milli1 to 1000 then 1000 to milli2 and subtract 1000 to make up
		'for the 1 second this process adds.
		millidiff = (1000 - CInt(milli1)) + CInt(milli2) - 1000
	End If
	GetMilliDiff = CStr(timediff + millidiff)
End Function

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
Here is my VB code:
Public Seconds As Double
Public Minutes As Double
Public Hours As Double

Public Sub MSCalc(StartTime As String, Endtime As String)
If StartTime > Endtime Then
strStartTime = Endtime
strEndtime = StartTime
Else
strStartTime = StartTime
strEndtime = Endtime
End If

startArray = Split(strStartTime, ".")
EndArray = Split(strEndtime, ".")

Dim i As Integer
Dim startMS As Integer, endMS As Long

MainStartTime = CDate(startArray(0))
startMS = CInt(startArray(1))

MainEndTime = CDate(EndArray(0))
endMS = CInt(EndArray(1))

Dim diff As Double
Dim totaltime As Long

diff = DateDiff("s", MainStartTime, MainEndTime)

If diff = 0 Then
totaltime = endMS - startMS
Else
endMS = endMS + (1000 * diff)
totaltime = endMS - startMS
End If

Seconds = Round(totaltime / 1000, 3)
Minutes = Round((totaltime / 1000) / 60, 2)

If (totaltime / 1000) / 60 > 59 Then
Hours = Round(((totaltime / 1000) / 60) / 60, 2)
End If
End Sub

Here is my VBS code to call and execute:
Dim objMSCalc
Set objMSCalc = CreateObject("RMT.CalcMS")
call objMSCalc.MSCalc("12:50:45.267","12:50:45.250")
WScript.Echo objMSCalc.Seconds & " seconds"
WScript.Echo objMSCalc.Minutes & " minutes"
WScript.Echo objMSCalc.Hours & " hours"
Set objMSCalc = nothing

Here is my ouput from my VBScode
0.017 seconds
0 minutes
0 hours
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top