Current Unix Timestamp SECONDS SINCE JAN 01 1970

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

How to convert Datetime to Unix timestamp in Erlang

🎈 🎈 🎈
+1

    % Convert DateTime to Unix timestamp
    DateTime = {{2022, 5, 15}, {8, 30, 45}},
    UnixTimestamp = calendar:datetime_to_unix(DateTime),
    io:format("Unix Timestamp: ~p~n", [UnixTimestamp]).            
        

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

Code Explanation

In this Erlang example, the datetime tuple {{2022, 5, 15}, {8, 30, 45}} (representing 2022-05-15 08:10:45) is passed to the calendar:datetime_to_unix() function. This function calculates the number of seconds since January 1, 1970 (Unix epoch) and returns the Unix timestamp.
The result is 1652602245.



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