Current Unix Timestamp SECONDS SINCE JAN 01 1970

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

How to convert Unix timestamp to Datetime in Zig

🎈 🎈 🎈
+1

    const std = @import("std");

    pub fn main() void {
        const unix_timestamp: i64 = 911555421;
    
        // Convert Unix timestamp to Date
        const date = std.time.gmtime(unix_timestamp);
    
        // Format the date to a readable string
        std.debug.print("Converted Date: {}-{}-{} {}:{}:{}\n", 
            .{date.year, date.month + 1, date.day, date.hour, date.minute, date.second});
    }            
        

Output:
Converted Date: 1998-11-20 09:50:21
Example only. There may be multiple ways to perform this operation.

Code Explanation

In this snippet to perform Unix timestamp to date conversion in Zig, std.time.gmtime(unix_timestamp) converts the Unix timestamp 911555421 to a std.time.Date object, interpreting the timestamp in UTC.
The date components (year, month, day, hour, minute, second) are accessed directly from the Date object and printed using std.debug.print().
The result is printed in a readable format like YYYY-MM-DD HH:MM:SS, in this case 1998-11-20 09:50:21.


Other useful Zig date functions

Function Description Example
std.time.gmtime() Converts a Unix timestamp to a std.time.Date object in UTC. std.time.gmtime(1514764800) returns a Date object for 2018-01-01 00:00:00 UTC
std.time.timegm() Converts a std.time.Date object to a Unix timestamp. std.time.timegm(date) returns 1531656645 (for July 15, 2018).
Date A structure representing a date and time in Zig. Contains fields like year, month, day, hour, minute, and second. Date{ .year = 2018, .month = 6, .day = 15, .hour = 12, .minute = 30, .second = 45 }
std.time.now() Returns the current Unix timestamp. std.time.now() returns the current Unix timestamp.
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