Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-04-25
<title>Convert Unix Timestamp to Datetime in AutoHotkey

How to convert Unix timestamp to Datetime in AutoHotkey

🎈 🎈 🎈
+1

    ; Unix timestamp (for example, 1652602245, which represents 2022-05-15 08:10:45)
    UnixTimestamp := 1652602245
    
    ; Convert Unix timestamp to milliseconds (AutoHotkey uses milliseconds, Unix timestamps are in seconds)
    Milliseconds := UnixTimestamp * 1000
    
    ; Set the epoch date to "1970-01-01 00:00:00"
    Epoch := "19700101000000"
    
    ; Add the milliseconds to the epoch
    EnvAdd, Epoch, %Milliseconds%, Milliseconds
    
    ; Format the date into human-readable form
    FormatTime, HumanDate, %Epoch%, yyyy-MM-dd HH:mm:ss
    
    MsgBox, Unix Timestamp: %UnixTimestamp%`nConverted Date: %HumanDate%            
        

Output:
Unix Timestamp: 1652602245 Converted Date: 2022-05-15 08:10:45
Example only. There may be multiple ways to perform this operation.

Code Explanation

In AutoHotkey, a Unix timestamp (which represents the number of seconds since January 1, 1970) can be converted to a human-readable date by first multiplying the timestamp by 1000 to convert it into milliseconds (since AutoHotkey operates with milliseconds). Then, we set an initial date to the Unix epoch ("1970-01-01 00:00:00") and use EnvAdd to add the calculated milliseconds to the epoch.
This gives us the correct date and time in AutoHotkey's internal format. Finally, the FormatTime function is used to transform the internal date format into a readable format, such as yyyy-MM-dd HH:mm:ss. The example script converts the Unix timestamp '1652602245' to the date '2022-05-15 08:10:45'.



Other useful AutoHotkey date functions

Function/Command Description Example
A_Now Returns the current date and time in the format YYYYMMDDHH24MISS. MsgBox % A_Now returns 20240828123456
A_NowUTC Returns the current date and time in UTC in the format YYYYMMDDHH24MISS. MsgBox % A_NowUTC returns 20240828123456
FormatTime Formats a timestamp into a human-readable date and time string. FormatTime, OutputVar, %A_Now%, yyyy-MM-dd HH:mm:ss returns 2024-08-28 12:34:56
EnvAdd Adds a specified amount of time to a timestamp (e.g., days, hours, minutes). EnvAdd, A_Now, 5, Days adds 5 days to the current date.
EnvSub Subtracts a specified amount of time from a timestamp (e.g., days, hours, minutes). EnvSub, A_Now, 2, Hours subtracts 2 hours from the current date.
FileSetTime Sets the modification time of a file to a specific date and time. FileSetTime, %A_Now%, "example.txt" sets the file's modification time to the current time.
FileGetTime Retrieves the modification time of a file. FileGetTime, OutputVar, "example.txt" stores the file's timestamp in OutputVar.
A_TickCount Returns the number of milliseconds since the system was booted. MsgBox % A_TickCount returns 123456789
SetTimer Creates a timer that performs an action after a specified interval. SetTimer, MyTimerLabel, 1000 runs a label every 1 second.
Sleep Pauses the script for a specified number of milliseconds. Sleep, 5000 pauses for 5 seconds.
Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-04-25
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder