I want to convert a timestamp to my local time, but there's very few posts I could find on this subject.
The link below is the best that I've found, however it's from year 2011:
https://autohotkey.com/board/topic/68856-sample-dealing-with-time-zones-ahk-l/?p=690830
Are there any improved functions now to achieve this? The post I already found might be the best method, but I wanted to ask if anyone has anything better.
Thanks!
Current TimeZone: (UTC-06:00) Central Time (US & Canada), adjusting for daylight saving time
1560516182 = 7:43 AM Friday, June 14, 2019
Convert Timestamp to Local Time Topic is solved
Re: Convert Timestamp to Local Time
What kind of timestamp?
Re: Convert Timestamp to Local Time
Like this:
Current TimeZone: (UTC-06:00) Central Time (US & Canada), adjusting for daylight saving time
1560516182 = 7:43 AM Friday, June 14, 2019
Pasting that timestamp in the script I linked in my previous post returns the correct date/time I want.
Using this website also gets the correct date from that timestamp. https://www.epochconverter.com/
Re: Convert Timestamp to Local Time Topic is solved
Here are various bits of code to do the conversion.
Two examples: Unix date to AHK (local date based on current time zone).
One example: Unix date to AHK (local date based on time zone at the time).
And a FormatTime example. Cheers.
Two examples: Unix date to AHK (local date based on current time zone).
One example: Unix date to AHK (local date based on time zone at the time).
And a FormatTime example. Cheers.
Code: Select all
q:: ;convert Unix date to local AHK date (based on current time zone)
vSec := 1560516182
;vDate := 1970
;EnvAdd, vDate, % vSec, Seconds
;MsgBox, % vDate
vIntervalsUTC := 116444736000000000 ;1 Jan 1970 as a FILETIME
vIntervalsUTC += vSec * 10000000
vIntervalsLocal := 0
DllCall("kernel32\FileTimeToLocalFileTime", "Int64*",vIntervalsUTC, "Int64*",vIntervalsLocal)
vDate := 1601
EnvAdd, vDate, % vIntervalsLocal//10000000, Seconds
MsgBox, % vDate
return
w:: ;convert Unix date to local AHK date (based on current time zone) (alternative)
vSec := 1560516182
vDate := 1970
EnvAdd, vDate, % vSec, Seconds
;MsgBox, % vDate
VarSetCapacity(SYSTEMTIME, 16, 0)
vDate := RegExReplace(vDate, "(....)(..)(..)(..)(..)(..)", "$1 $2 0 $3 $4 $5 $6")
Loop Parse, vDate, % " "
NumPut(A_LoopField, &SYSTEMTIME, A_Index*2-2, "UShort")
vIntervalsUTC := vIntervalsLocal := 0
DllCall("kernel32\SystemTimeToFileTime", "Ptr",&SYSTEMTIME, "Int64*",vIntervalsUTC)
DllCall("kernel32\FileTimeToLocalFileTime", "Int64*",vIntervalsUTC, "Int64*",vIntervalsLocal)
vDate := 1601
EnvAdd, vDate, % vIntervalsLocal//10000000, Seconds
MsgBox, % vDate
return
e:: ;convert Unix date to local AHK date (based on time zone at the time)
vSec := 1560516182
vDate := 1970
EnvAdd, vDate, % vSec, Seconds
;MsgBox, % vDate
VarSetCapacity(SYSTEMTIME1, 16, 0)
VarSetCapacity(SYSTEMTIME2, 16, 0)
vDate := RegExReplace(vDate, "(....)(..)(..)(..)(..)(..)", "$1 $2 0 $3 $4 $5 $6")
Loop Parse, vDate, % " "
NumPut(A_LoopField, &SYSTEMTIME1, A_Index*2-2, "UShort")
DllCall("kernel32\SystemTimeToTzSpecificLocalTime", "Ptr",0, "Ptr",&SYSTEMTIME1, "Ptr",&SYSTEMTIME2)
vDate := ""
Loop 7
if !(A_Index = 3)
vDate .= Format("{:02}", NumGet(&SYSTEMTIME2, A_Index*2-2, "UShort"))
MsgBox, % vDate
return
r:: ;convert date format
vDate := 20190614074302
FormatTime, vDate, % vDate, % "h:mm tt dddd, MMMM d, yyyy"
MsgBox, % vDate
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA