Current Unix Timestamp SECONDS SINCE JAN 01 1970

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

How to convert Datetime to Unix timestamp in Zig

🎈 🎈 🎈
+1

    const std = @import("std");

    pub fn main() void {
        // Create a Date object for 1989-03-29 17:28:25 UTC
        const date = std.time.Date{
            .year = 1989,
            .month = 3,  // Zig months are 0-based, so July is 6
            .day = 29,
            .hour = 17,
            .minute = 28,
            .second = 25,
        };
    
        // Convert Date to Unix timestamp
        const unix_timestamp = std.time.timegm(date);
    
        // Print the Unix timestamp
        std.debug.print("Unix Timestamp: {}\n", .{unix_timestamp});
    }            
        

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

Code Explanation

It this example of date to Unix timestamp conversion in Zig, a std.time.Date object is created for '1989-03-29 17:28:25' using the Date structure.
The std.time.timegm(date) function calculates the Unix timestamp from the provided Date object.
The Unix timestamp is printed using std.debug.print().



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