Current Unix Timestamp SECONDS SINCE JAN 01 1970

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

How to convert Datetime to Unix timestamp in Vala

🎈 🎈 🎈
+1

    void main () {
        // Create a DateTime object from a specific date and time (2010-09-16 23:24:43)
        GLib.DateTime date = GLib.DateTime.new_local(2010, 9, 16, 23, 24, 43);
    
        // Convert DateTime to Unix timestamp
        int64 unix_timestamp = date.to_unix();
    
        print("Unix Timestamp: %" + int64.FORMAT + "\n", unix_timestamp);
    }            
        

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

Code Explanation

In this example of date to Unix timestamp in Vala GLib.DateTime.new_local(2010, 9, 16, 23, 24, 43) creates a DateTime object representing the date '2010-09-16 23:24:43' in the local time zone. Subsequently date.to_unix() converts the DateTime object to a Unix timestamp. The result is printed using print.



Other useful Vala date functions (GLib.DateTime)

Function Description Example
GLib.DateTime.new_from_unix_local() Creates a DateTime object from a Unix timestamp, interpreted as local time. GLib.DateTime.new_from_unix_local(1514764800) returns 2018-01-01 00:00:00
GLib.DateTime.to_unix() Converts a DateTime object to a Unix timestamp. date.to_unix() returns 1531656645
GLib.DateTime.new_local() Creates a new DateTime object with the specified date and time in the local time zone. GLib.DateTime.new_local(2018, 7, 15, 12, 30, 45) creates the 2018-07-15 12:30:45 date.
GLib.DateTime.format() Formats a DateTime object as a string according to a specified format. date.format("%Y-%m-%d %H:%M:%S") returns 2018-07-15 12:30:45
GLib.DateTime.now_local() Returns the current date and time as a DateTime object in the local time zone. GLib.DateTime.now_local() returns the current date and time.
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