VBA - show clock time with accuracy of less than a second

前端 未结 5 1932
不知归路
不知归路 2020-12-11 08:59

Is there a way to use VBA (excel) to generate a clock time with accuracy to a tenth of a second or less?

eg:

Sub test()
    MsgBox Format(Time, \"hh:         


        
5条回答
  •  时光取名叫无心
    2020-12-11 09:34

    The following VBA code returns the current local time as a String, including milliseconds. If you need system time, simply replace GetLocalTime by GetSystemTime.

    Private Type SYSTEMTIME
      wYear          As Integer
      wMonth         As Integer
      wDayOfWeek     As Integer
      wDay           As Integer
      wHour          As Integer
      wMinute        As Integer
      wSecond        As Integer
      wMilliseconds  As Integer
    End Type
    
    Private Declare Sub GetLocalTime Lib "kernel32" (ByRef lpLocalTime As SYSTEMTIME)
    
    Public Function NowMilli() As String
    Dim tTime As SYSTEMTIME
    Dim sTwo As String, sThree As String
    Dim sOut As String
       sOut = "yyyy-mm-dd hh:mm:ss.mmm"
       sTwo = "00": sThree = "000"
       Call GetLocalTime(tTime)
       Mid(sOut, 1, 4) = tTime.wYear
       Mid(sOut, 6, 2) = Format(tTime.wMonth, sTwo)
       Mid(sOut, 9, 2) = Format(tTime.wDay, sTwo)
       Mid(sOut, 12, 2) = Format(tTime.wHour, sTwo)
       Mid(sOut, 15, 2) = Format(tTime.wMinute, sTwo)
       Mid(sOut, 18, 2) = Format(tTime.wSecond, sTwo)
       Mid(sOut, 21, 3) = Format(tTime.wMilliseconds, sThree)
       NowMilli = sOut
    
    End Function
    

提交回复
热议问题