我想用autohotkey获取自午夜以来的当前时间(以毫秒为单位)

时间:2018-10-06 14:09:27

标签: autohotkey

我想获取自午夜以来的当前时间(以毫秒为单位)。

我完全不知道这可能吗?

我知道 A_TickCount 给出了自重启计算机以来的毫秒数。

2 个答案:

答案 0 :(得分:2)

要获取以毫秒为单位的当前时间,可以使用Winapi函数GetSystemTime(UTC日期)或GetLocalTime(本地日期)。

;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
}

要获取以秒为单位的当前时间,可以解析内置变量A_Now / A_NowUTC,或使用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

答案 1 :(得分:1)

请参见DateTime
可以从当前时间像这样计算毫秒:

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