Convert Timestamp to Local Time Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
TheDewd
Posts: 1352
Joined: 19 Dec 2013, 11:16
Location: USA

Convert Timestamp to Local Time

14 Jun 2019, 08:30

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
Image Bulldozer - Sokoban inspired game from 1994 recreated in AutoHotkey.
just me
Posts: 6551
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Convert Timestamp to Local Time

14 Jun 2019, 09:58

What kind of timestamp?
User avatar
TheDewd
Posts: 1352
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Convert Timestamp to Local Time

14 Jun 2019, 10:01

just me wrote:
14 Jun 2019, 09:58
What kind of timestamp?

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/
Image Bulldozer - Sokoban inspired game from 1994 recreated in AutoHotkey.
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Convert Timestamp to Local Time  Topic is solved

14 Jun 2019, 15:10

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.

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

Return to “Ask For Help”

Who is online

Users browsing this forum: Albireo, Bing [Bot], pn4265 and 253 guests