首页 文章

使用VBScript查找毫秒的时间

提问于
浏览
9

我希望得到毫秒的时间目前使用Timer()方法,但它只是提供访问到第二个任何想法?

请确保我不想将秒转换为毫秒,希望得到毫秒

2 回答

  • 0

    事实上,Timer函数为您提供毫秒的秒数 . 返回值的整数部分是自午夜以来的秒数,小数部分可以转换为毫秒 - 只需乘以1000即可 .

    t = Timer
    
    ' Int() behaves exacly like Floor() function, ie. it returns the biggest integer lower than function's argument
    temp = Int(t)
    
    Miliseconds = 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, Miliseconds
    
    ' 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(Miliseconds), "0") & Miliseconds
    
    WScript.Echo strTime
    
  • 14

    基于MBu的答案,这是一个Sub版本 . 在您的代码周围为即时窗口中的消息调用此调用,以便您可以查看延迟发生的位置 .

    ' *** 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
    

相关问题