Current Unix Timestamp SECONDS SINCE JAN 01 1970

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

How to convert Datetime to Unix timestamp in Gleam

🎈 🎈 🎈
+1

    import gleam/time

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

Output:
1052603445
Example only. There may be multiple ways to perform this operation.

Code Explanation

In this Gleam snippet, time.to_unix_time is used to convert a time.Time structure back into a Unix timestamp.
This function returns an integer that represents the number of seconds since the Unix epoch (January 1, 1970).
Gleam's time.Time structure holds the date-time components and can be converted back into this standard format. For the date value of '2003-05-10 21:50:45', a Unix timestamp of '1052603445' would be returned.



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