Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Unix time milliseconds

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?

like image 987
Dmitrij Holkin Avatar asked Apr 21 '15 12:04

Dmitrij Holkin


People also ask

How do I get milliseconds in Unix?

date +"%T. %6N" returns the current time with nanoseconds rounded to the first 6 digits, which is microseconds. date +"%T. %3N" returns the current time with nanoseconds rounded to the first 3 digits, which is milliseconds.

How do you get Unix timestamp in seconds *?

To find the unix current timestamp use the %s option in the date command. The %s option calculates unix timestamp by finding the number of seconds between the current date and unix epoch.

How do you find the current time in milliseconds?

To get the current time in milliseconds, you just need to convert the output of Sys. time to numeric, and multiply by 1000. Depending on the API call you want to make, you might need to remove the fractional milliseconds.


1 Answers

Here is a short extension on the answer by @bouvierr as I needed the equivalent of the java.lang.System.currentTimeMillis() method in VBA:

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
like image 149
David Robson Avatar answered Oct 27 '22 13:10

David Robson