SL5net
SL5net

Reputation: 2556

i want, with autohotkey, get current time in milliseconds since midnight

i want get current time in milliseconds since midnight.

I do not know if this is possible at all?

I know A_TickCount gives number of milliseconds since the computer was rebooted.

Upvotes: 2

Views: 4086

Answers (2)

Mikhail V
Mikhail V

Reputation: 1521

See DateTime
Milliseconds can be calculated from current time like this:

F1::
    millis := (a_hour*3600 + a_min*60 + a_sec)*1000 + a_msec
    tooltip %millis%
return

Upvotes: 6

vafylec
vafylec

Reputation: 1015

To get the current time in milliseconds, you can use the Winapi functions GetSystemTime (UTC date) or GetLocalTime (local date).

;get current time in milliseconds (since midnight)
vDate := RegExReplace(A_Now, "(?<=..)..(?=.)", "$0 ")
vDateUTC := RegExReplace(A_NowUTC, "(?<=..)..(?=.)", "$0 ")
vMSec := JEE_TimeNowMSec()
vMSecUTC := JEE_TimeNowMSec("UTC")
vOutput1 := vDate "`r`n" vMSec "`r`n" (vMSec/86400000)
vOutput2 := vDateUTC "`r`n" vMSecUTC "`r`n" (vMSecUTC/86400000)
MsgBox, % vOutput1 "`r`n`r`n" vOutput2

JEE_TimeNowMSec(vOpt:="")
{
    VarSetCapacity(SYSTEMTIME, 16, 0)
    if (vOpt = "UTC")
        DllCall("kernel32\GetSystemTime", Ptr,&SYSTEMTIME)
    else
        DllCall("kernel32\GetLocalTime", Ptr,&SYSTEMTIME)
    vHour := NumGet(&SYSTEMTIME, 8, "UShort") ;wHour
    vMin := NumGet(&SYSTEMTIME, 10, "UShort") ;wMinute
    vSec := NumGet(&SYSTEMTIME, 12, "UShort") ;wSecond
    vMSec := NumGet(&SYSTEMTIME, 14, "UShort") ;wMilliseconds
    return vHour*3600000 + vMin*60000 + vSec*1000 + vMSec
}

To instead get the current time in seconds, you can parse the built-in variables A_Now/A_NowUTC, or use FormatTime.

;get current time in seconds (since midnight)
vDate := RegExReplace(A_Now, "(?<=..)..(?=.)", "$0 ")
vDateUTC := RegExReplace(A_NowUTC, "(?<=..)..(?=.)", "$0 ")
oDate := StrSplit(vDate, " ")
oDateUTC := StrSplit(vDateUTC, " ")
vSec := oDate.4*3600 + oDate.5*60 + oDate.6
vSecUTC := oDateUTC.4*3600 + oDateUTC.5*60 + oDateUTC.6
vOutput1 := vDate "`r`n" vSec "`r`n" (vSec/86400)
vOutput2 := vDateUTC "`r`n" vSecUTC "`r`n" (vSecUTC/86400)
MsgBox, % vOutput1 "`r`n`r`n" vOutput2
return

;get current time in seconds (since midnight)
vDate := RegExReplace(A_Now, "(?<=..)..(?=.)", "$0 ")
vDateUTC := RegExReplace(A_NowUTC, "(?<=..)..(?=.)", "$0 ")
FormatTime, vDateTemp,, HH:mm:ss
FormatTime, vDateTempUTC, % A_NowUTC, HH:mm:ss
oTime := StrSplit(vDateTemp, ":")
oTimeUTC := StrSplit(vDateTempUTC, ":")
vSec := oTime.1*3600 + oTime.2*60 + oTime.3
vSecUTC := oTimeUTC.1*3600 + oTimeUTC.2*60 + oTimeUTC.3
vOutput1 := vDate "`r`n" vSec "`r`n" (vSec/86400)
vOutput2 := vDateUTC "`r`n" vSecUTC "`r`n" (vSecUTC/86400)
MsgBox, % vOutput1 "`r`n`r`n" vOutput2
return

Upvotes: 2

Related Questions