获取Unix时间毫秒

在Java中以毫秒为单位获得系统时间,我使用:

new date().gettime() 

使用Excel VBA可以以毫秒为单位获得相同的结果?

摘要:为获得最佳结果,请使用GetSystemTime

Excel工作表函数Now()具有相对较好的精确度,大概低至10毫秒。 但要称之为必须使用工作表公式。

要正确获取毫秒值,您应该避免 VBA Now()函数。 它的精度大概是1秒。

VBA Timer()函数返回一个约5毫秒的精度。 但是你必须使用Now()来获取date部分。 如果午夜之前调用Now()并且午夜之后调用Timer() (这对于大多数人来说可能是罕见的情况而不是问题),这可能会引起轻微的问题。

Windows API函数GetSystemTime具有真正的毫秒精度。 您可以使用SYSTEMTIME结构中的值来创build具有正确的毫秒精度的Excel双精度值。 GetSystemTime返回UTC时间,所以如果你需要POSIX格式的date,你可以减去UNIX纪元(1970年1月1日UTC),这是Exceldate格式的25569(不考虑闰秒)。

下面的代码比较每种方法的精度:

 Option Explicit 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 GetSystemTime Lib "kernel32" (lpSystemTime As SYSTEMTIME) Function Now_System() As Double Dim st As SYSTEMTIME GetSystemTime st Now_System = DateSerial(st.wYear, st.wMonth, st.wDay) + _ TimeSerial(st.wHour, st.wMinute, st.wSecond) + _ st.wMilliseconds / 86400000# End Function Function Now_Timer() As Double Now_Timer = CDbl(Int(Now)) + CDbl(Timer() / 86400#) End Function Sub CompareCurrentTimeFunctions() ' Compare precision of different methods to get current time. Me.Range("A1:D1000").NumberFormat = "yyyy/mm/dd h:mm:ss.000" Dim d As Double Dim i As Long For i = 2 To 1000 ' 1) Excel NOW() formula returns same value until delay of ~10 milliseconds. (local time) Me.Cells(1, 1).Formula = "=Now()" d = Me.Cells(1, 1) Me.Cells(i, 1) = d ' 2) VBA Now() returns same value until delay of ~1 second. (local time) d = Now Me.Cells(i, 2) = d ' 3) VBA Timer returns same value until delay of ~5 milliseconds. (local time) Me.Cells(i, 3) = Now_Timer ' 4) System time is precise down to 1 millisecond. (UTC) Me.Cells(i, 4) = Now_System Next i End Sub 

不同的解释,基于Excel的posix时间和夏令时的小时调整:

 Sub Pose() ut = ((Now - 25569) * 86400000) - 3600000 End Sub 

如果不够精确,可能会对http://vbadud.blogspot.co.uk/2008/10/excel-vba-timestamp-milliseconds-using.html感兴趣。

下面是@bouvierr的答案的简短扩展,因为我需要VBA中的java.lang.System.currentTimeMillis()方法的等价物:

 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 GetSystemTime Lib "kernel32" (lpSystemTime As SYSTEMTIME) Function CurrentTimeMillis() As Double ' Returns the milliseconds from 1970/01/01 00:00:00.0 to system UTC Dim st As SYSTEMTIME GetSystemTime st Dim t_Start, t_Now t_Start = DateSerial(1970, 1, 1) ' Starting time for Linux t_Now = DateSerial(st.wYear, st.wMonth, st.wDay) + _ TimeSerial(st.wHour, st.wMinute, st.wSecond) CurrentTimeMillis = DateDiff("s", t_Start, t_Now) * 1000 + st.wMilliseconds End Function 

这会产生一个格式为yyyy mm dd hh:mm:ss.fff的时间戳,其中fff是毫秒。

 Dim dateToday As Date Dim datetimeNow As Date Dim secondsElapsedSinceMidnight As Double Dim h As Long Dim m As Long Dim s As Long dateToday = Now secondsElapsedSinceMidnight = Timer h = Int(secondsElapsedSinceMidnight / 3600) m = Int(secondsElapsedSinceMidnight / 60) - h * 60 s = Int(secondsElapsedSinceMidnight) - m * 60 - h * 3600 datetimeNow = DateSerial(Year(dateToday), Month(dateToday), Day(dateToday)) _ + TimeSerial(h, m, s) Debug.Print Format(datetimeNow, "yyyy mm dd hh:nn:ss.") _ & Format((secondsElapsedSinceMidnight _ - Int(secondsElapsedSinceMidnight)) * 1000, "000") 

当我提交这个答案时,输出是:

 2015 04 21 16:24:22.852 

我发现只有一个可能的变种

 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" (lpSystemTime As SYSTEMTIME) Sub test() Dim sSysTime As SYSTEMTIME GetLocalTime sSysTime MsgBox = ((Now - 25569) * 86400000) - 3600000 + sSysTime.wMilliseconds End Sub