Current Unix Timestamp SECONDS SINCE JAN 01 1970

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

How to convert Unix timestamp to Datetime in Nim

🎈 🎈 🎈
+1

    import times

    # Convert Unix timestamp to Date
    let unixTimestamp: int64 = 1721829396
    let date = fromUnix(unixTimestamp)
    
    echo "Converted Date: ", date.format("yyyy-MM-dd HH:mm:ss")            
        

Output:
Converted Date: 2024-07-24 13:56:36
Example only. There may be multiple ways to perform this operation.

Code Explanation

In order to convert a Unix timestamp to a date in Nim the Unix timestamp 1721829396 is passed to the fromUnix function, which converts the Unix timestamp into a Time object.
The format function is then used to format the Time object into a readable string, which is then printed as 2024-07-24 13:56:36.



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