Mulesoft Developer
Mulesoft Developer

Reputation: 2824

Find time with millisecond using VBScript

I want to get time with millisecond Currently using Timer() method but it just give access upto second Any idea?

Please make sure i don't want to convert second into millisecond want to get with millisecond

Upvotes: 15

Views: 42386

Answers (3)

Antoni Gual Via
Antoni Gual Via

Reputation: 763

This uses the standard dateserial in vbs

'get a timeserial with milliseconds
d1= date+ timer/86400
Wscript.echo datestamp(d1)&" "& timestamp(d1)

function datestamp(d)
  datestamp=year(d) &"/"& month(d) & "/" & day(d)
 end function

function timestamp(d)
  ymd=fix(d)
  h=hour(d)
  m=minute(d)
  s=second(d)
  timestamp=right("00" & h,2)&":"& right("00" & m,2)& ":" & right("00" & s,2)& ","& _
  right("000"& 86400000*(d-timeserial(h,m,s)-ymd),3)
end function  

Upvotes: 1

MBu
MBu

Reputation: 2950

In fact Timer function gives you seconds with milliseconds. Integer part of returned value is the number of seconds since midnight and the fraction part can be converted into milliseconds - just multiply it by 1000.

t = Timer

' Int() behaves exactly like Floor() function, i.e. it returns the biggest integer lower than function's argument
temp = Int(t)

Milliseconds = Int((t-temp) * 1000)

Seconds = temp mod 60
temp    = Int(temp/60)
Minutes = temp mod 60
Hours   = Int(temp/60)

WScript.Echo Hours, Minutes, Seconds, Milliseconds

' Let's format it
strTime =           String(2 - Len(Hours), "0") & Hours & ":"
strTime = strTime & String(2 - Len(Minutes), "0") & Minutes & ":"
strTime = strTime & String(2 - Len(Seconds), "0") & Seconds & "."
strTime = strTime & String(4 - Len(Milliseconds), "0") & Milliseconds

WScript.Echo strTime

Upvotes: 20

AjV Jsy
AjV Jsy

Reputation: 6075

Building upon MBu's answer, here's a Sub version. Sprinkle calls to this around your code for messages in the immediate window, so you can see where delays are happening.

' *** Debug.Print the time with milliseconds, and a message of your choice
Private Sub DebugPrintTime(strWhereFrom As String)
On Error GoTo ErrHandler

    Dim sglTimer As Single
    Dim sglWholeSecs As Single
    Dim Millisecs As Variant  ' as a variant, Len() will give the length of string representation of this value
    Dim Seconds As Variant
    Dim Minutes As Variant
    Dim Hours As Variant
    Dim strTime As String

    sglTimer = timer
    sglWholeSecs = Int(sglTimer)
    Millisecs = Int((sglTimer - sglWholeSecs) * 1000)
    Seconds = sglWholeSecs Mod 60
    sglWholeSecs = Int(sglWholeSecs / 60)
    Minutes = sglWholeSecs Mod 60
    Hours = Int(sglWholeSecs / 60)

    strTime = String(2 - Len(Hours), "0") & Hours & ":"
    strTime = strTime & String(2 - Len(Minutes), "0") & Minutes & ":"
    strTime = strTime & String(2 - Len(Seconds), "0") & Seconds & "."
    strTime = strTime & String(3 - Len(Millisecs), "0") & Millisecs
    Debug.Print strTime, strWhereFrom

    Exit Sub
ErrHandler:
    MsgBox "Error in Sub DebugPrintTime" & vbCrLf & Err.Description & vbCrLf & strWhereFrom
    Err.Clear
End Sub

Upvotes: -1

Related Questions