Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-08-21
Convert Unix Timestamp to Datetime in Gleam

How to convert Unix timestamp to Datetime in Gleam

🎈 🎈 🎈
+1

    import gleam/time

    pub fn unix_to_date(unix_timestamp: Int) -> time.Time {
        let date = time.from_unix_time(unix_timestamp)
        date
    }                    
        

Output:
1998-12-29 02:41:59
Example only. There may be multiple ways to perform this operation.

Code Explanation

In this Gleam example, time.from_unix_time is used to convert the provided Unix timestamp (which represents the number of seconds since January 1, 1970) to a time.Time structure.
The time.Time structure contains details like year, month, day, hours, and minutes.
In this example, the Unix timestamp '914899319' would be transformed into '1998-12-29 02:41:59'



Other useful Gleam date functions

Function Description Example
time.now() Returns the current time as a time.Time type. time.now() returns Time{2024, 9, 5, 14, 55, 0}
time.from_unix_time(seconds) Converts a Unix timestamp to a time.Time structure. time.from_unix_time(1609459200) returns Time{2021, 1, 1, 0, 0, 0}
time.to_unix_time(time) Converts a time.Time structure to a Unix timestamp. time.to_unix_time(date) returns 1609459200
time.add_seconds(time, seconds) Adds a number of seconds to a time.Time structure. time.add_seconds(now, 3600) adds 1 hour to the current time.
time.diff_seconds(a, b) Calculates the difference in seconds between two time.Time structures. time.diff_seconds(time1, time2) returns the difference in seconds between two times.
Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-08-21
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder