Current Unix Timestamp SECONDS SINCE JAN 01 1970

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

How to convert Datetime to Unix timestamp in V

🎈 🎈 🎈
+1

    import time

    fn main() {
        // Create a date (time.Time structure) for 2010-09-17 02:11:23
        date := time.Time{
            year: 2010
            month: 9
            day: 17
            hour: 2
            minute: 11
            second: 23
        }
    
        // Convert Date to Unix timestamp
        unix_timestamp := date.unix_time()
    
        println("Unix Timestamp: $unix_timestamp")
    }            
        

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

Code Explanation

In this V snippet for converting a date to Unix timestamp the time.Time structure is initialized with the year, month, day, hour, minute, and second representing the date '2010-09-17 02:11:23'. The unix_time() method is then called on the time.Time object to retrieve the corresponding Unix timestamp. The result is printed as a Unix timestamp.


Other useful V date functions

Function Description Example
time.unix() Converts a Unix timestamp into a time.Time object. time.unix(1514764800) returns Time{2018, 1, 1, 0, 0, 0}
Time.unix_time() Returns the Unix timestamp for a given time.Time object. date.unix_time() returns 1531656645 (for July 15, 2018).
Time.format() Formats a time.Time object into a custom string format. date.format('yyyy-MM-dd HH:mm:ss') returns 2018-07-15 12:30:45
time.now() Returns the current date and time as a time.Time object. time.now() returns the current date and time.
time.Time{} Creates a time.Time object with the specified date and time values. time.Time{year: 2018, month: 7, day: 15, ...} creates a Time object for 2018-07-15 12:30:45.
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