在Java中获取系统时间(以毫秒为单位)我使用:
new date().gettime()
使用Excel VBA可以在几毫秒内获得相同的结果吗?
答案 0 :(得分:4)
摘要:要获得最佳效果,请使用GetSystemTime
。
Excel工作表函数Now()
具有相对较好的精度,大致低至10毫秒。但要调用它,您必须使用工作表公式。
要正确获取毫秒值,您应避免 VBA Now()
功能。它的精度大约是1秒。
VBA Timer()
函数返回single
,精度约为5毫秒。但是你必须使用Now()
来获取日期部分。如果在午夜之前将Now()
称为,在午夜之后将Timer()
称为,则可能会出现轻微问题(这可能是一种罕见的情况,而不是大多数人的问题。)
Windows API函数GetSystemTime具有真正的毫秒精度。您可以使用SYSTEMTIME结构中的值来创建具有正确毫秒精度的Excel双精度。 GetSystemTime返回UTC时间,所以如果你想要POSIX格式的日期,你可以减去UNIX纪元(1970年1月1日UTC),这是Excel日期格式的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
答案 1 :(得分:3)
基于Excel posix time的不同解释以及夏令时的小时调整:
Sub Pose()
ut = ((Now - 25569) * 86400000) - 3600000
End Sub
如果不够精确,可能会感兴趣http://vbadud.blogspot.co.uk/2008/10/excel-vba-timestamp-milliseconds-using.html。
答案 2 :(得分:2)
以下是@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
答案 3 :(得分:1)
这会生成格式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
答案 4 :(得分:1)
我发现只有一个可能的变种
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