获取Unix时间毫秒 [英] Get Unix time milliseconds
问题描述
new date()。gettime()
可以使用Excel VBA在毫秒内获得相同的结果?
摘要:为获得最佳效果,请使用 GetSystemTime
。
Excel工作表函数 Now()
具有相对较好的精度,大致低至10 ms。但是要调用它,你必须使用工作表公式。
要正确获取毫秒值,您应该避免 VBA Now()
功能。它的精度约为1秒。
VBA Timer()
函数返回一个
精度约为5毫秒。但是你必须使用 Now()
来获取日期部分。如果 Now()
在午夜之前调用, Timer()
在午夜后被称为(这可能是一个罕见的情况,而不是大多数人的问题)。
Windows API函数 GetSystemTime 具有真正的毫秒精度。您可以使用 SYSTEMTIME中的值结构来创建具有正确毫秒精度的Excel double。 GetSystemTime返回UTC时间,所以如果你想要POSIX格式的日期,你可以减去UNIX纪元(1970年1月1日UTC),这是25569的Excel日期格式(忽略闰秒)。
下面的代码比较了每个方法的精度:
Option Explicit
私有类型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
结束类型
私有声明Sub GetSystemTime Libkernel32(lpSystemTime As SYSTEMTIME)
函数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#
结束函数
函数Now_Timer()As Doub le
Now_Timer = CDbl(Int(Now))+ CDbl(Timer()/ 86400#)
结束函数
Sub CompareCurrentTimeFunctions()
比较不同方法的精度来获取当前时间。
Me.Range(A1:D1000)。NumberFormat =yyyy / mm / dd h:mm:ss.000
Dim d As Double
Dim i As Long
对于i = 2至1000
'1)Excel NOW()公式返回相同的值,直到延迟〜10毫秒。 (当地时间)
Me.Cells(1,1).Formula == Now()
d = Me.Cells(1,1)
Me.Cells(i,1) = d
'2)VBA Now()返回相同的值,直到延迟〜1秒。 (本地时间)
d =现在
Me.Cells(i,2)= d
'3)VBA Timer返回相同的值,直到延迟〜5毫秒。 (当地时间)
Me.Cells(i,3)= Now_Timer
'4)系统时间精确到1毫秒。 (UTC)
Me.Cells(i,4)= Now_System
Next i
End Sub
In Java to get system time in milliseconds I use:
new date().gettime()
It is possible to get the same result in milliseconds using Excel VBA?
SUMMARY: For best results, use GetSystemTime
.
The Excel worksheet function Now()
has relatively good precision, roughly down to 10 ms. But to call it you have to use a worksheet formula.
To correctly get the milliseconds value, you should avoid the VBA Now()
function. Its precision is roughly 1 second.
The VBA Timer()
function returns a single
with a precision of roughly 5 milliseconds. But you have to use Now()
to get the date part. This might cause a slight problem if Now()
is called before midnight and Timer()
is called after midnight (this is probably a rare situation and not an issue for most people).
The Windows API function GetSystemTime has true millisecond precision. You can use the values in the SYSTEMTIME structure to create an Excel double that has the correct millisecond precision. GetSystemTime returns the UTC time so if you want the date in POSIX format, you can subtract the UNIX epoch (1 January 1970 UTC), which is 25569 in Excel date format (disregarding leap seconds).
The code below compares the precision of each method:
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
这篇关于获取Unix时间毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!