Current Unix Timestamp SECONDS SINCE JAN 01 1970

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

How to convert Datetime to Unix timestamp in Nim

🎈 🎈 🎈
+1

    import times

    # Convert Date to Unix timestamp
    let date = initDateTime(1973, 11, 28, 14, 33, 38)
    let unixTimestamp = toUnix(date)
    
    echo "Unix Timestamp: ", unixTimestamp            
        

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

Code Explanation

In this method for converting a date to a Unix timestamp in Nim, a Time object is created for the date '1973-11-28 14:33:38' using initDateTime().
The toUnix function is then used to convert the Time object into a Unix timestamp, which represents the number of seconds since January 1, 1970. The result is '123345218', which is printed.



Other useful Nim date functions

Function Description Example
now() Returns the current date and time as a Time object. now() returns 2024-08-28 12:34:56
fromUnix() Converts a Unix timestamp (in seconds) to a Time object. fromUnix(1589530245) returns 2020-05-15 08:10:45
toUnix() Converts a Time object to a Unix timestamp (in seconds). toUnix(initDateTime(2020, 5, 15, 8, 30, 45)) returns 1589530245
initDateTime() Initializes a Time object for the given year, month, day, hour, minute, and second. initDateTime(2020, 5, 15, 8, 30, 45) creates 2020-05-15 08:10:45
format() Formats a Time object as a string using a specified format. time.format("yyyy-MM-dd HH:mm:ss") returns 2020-05-15 08:10:45
parse() Parses a date string and returns a Time object. parse("2020-05-15 08:10:45", "yyyy-MM-dd HH:mm:ss") returns a Time object.
addSeconds() Adds a number of seconds to a Time object. addSeconds(now(), 3600) adds 1 hour to the current time.
toLocal() Converts a UTC Time object to local time. now().toLocal() converts the current UTC time to local time.
dayOfWeek() Returns the day of the week (0=Sunday, 6=Saturday) for a Time object. dayOfWeek(now()) returns the current day of the week.
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