Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-08-20
Convert Datetime to Unix Timestamp in AutoHotkey

How to convert Datetime to Unix timestamp in AutoHotkey

🎈 🎈 🎈
+1

    ; Random date (for example, 2022-05-15 08:10:45)
    RandomDate := "20220515083045"
    
    ; Set the epoch date to "1970-01-01 00:00:00"
    Epoch := "19700101000000"
    
    ; Calculate the difference in seconds between the random date and the epoch
    EnvSub, RandomDate, %Epoch%, Seconds
    
    ; Convert the difference into a Unix timestamp (which is in seconds)
    UnixTimestamp := RandomDate
    
    ; Display only the Unix Timestamp in the message box
    MsgBox, Unix Timestamp: %UnixTimestamp%          
        

Output:
Unix Timestamp: 1652602245
Example only. There may be multiple ways to perform this operation.

Code Explanation

To convert a specific date to a Unix timestamp in AutoHotkey, you can calculate the difference between the given date and the Unix epoch (January 1, 1970).
AutoHotkey's EnvSub function allows you to subtract the epoch from the target date, yielding the number of seconds that have passed since the Unix epoch, which is effectively the Unix timestamp.
In the example script, the date '2022-05-15 08:10:45' is converted to its Unix timestamp by finding the difference between this date and 1970-01-01 00:00:00 in seconds. This difference is stored as the Unix timestamp.



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-08-20
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder