Current Unix Timestamp SECONDS SINCE JAN 01 1970

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

How to convert Unix timestamp to Datetime in Erlang

🎈 🎈 🎈
+1

    % Convert Unix timestamp to DateTime
    UnixTimestamp = 1122242312,
    DateTime = calendar:unix_time_to_datetime(UnixTimestamp),
    io:format("Converted Date: ~p~n", [DateTime]).            
        

Output:
Converted Date: 2005-07-24 21:58:32
Example only. There may be multiple ways to perform this operation.

Code Explanation

In this Erlang snippet, the Unix timestamp '1122242312' is passed to the calendar:unix_time_to_datetime() function. This function converts the Unix timestamp to a datetime tuple of the form {{Year, Month, Day}, {Hour, Minute, Second}}.
For this Unix timestamp, the result is {{2005, 7, 24}, {21, 58, 32}}, which represents the date '2005-07-24 21:58:32'.



Other useful Erlang date functions

Function Description Example
calendar:local_time() Returns the current local date and time as a tuple. calendar:local_time() returns {{2024, 8, 28}, {12, 34, 56}}
calendar:universal_time() Returns the current UTC date and time as a tuple. calendar:universal_time() returns {{2024, 8, 28}, {16, 34, 56}}
calendar:datetime_to_unix() Converts a `datetime` tuple to a Unix timestamp. calendar:datetime_to_unix({{2022, 5, 15}, {8, 30, 45}}) returns 1652602245
calendar:unix_time_to_datetime() Converts a Unix timestamp to a `datetime` tuple. calendar:unix_time_to_datetime(1652602245) returns {{2022, 5, 15}, {8, 30, 45}}
calendar:date_to_gregorian_days() Converts a date tuple to the number of days since year 0 in the Gregorian calendar. calendar:date_to_gregorian_days({2022, 5, 15}) returns 738187
calendar:gregorian_days_to_date() Converts the number of days since year 0 in the Gregorian calendar to a date tuple. calendar:gregorian_days_to_date(738187) returns {2022, 5, 15}
calendar:now_to_universal_time() Converts the current time to UTC as a `datetime` tuple. calendar:now_to_universal_time() returns the current UTC date and time.
calendar:time_to_seconds() Converts a time tuple to the number of seconds since midnight. calendar:time_to_seconds({8, 30, 45}) returns 30645 seconds
calendar:seconds_to_time() Converts a number of seconds since midnight to a time tuple. calendar:seconds_to_time(30645) returns {8, 30, 45}
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