Current Unix Timestamp SECONDS SINCE JAN 01 1970

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

How to convert Unix timestamp to Datetime in C++

🎈 🎈 🎈
+1

    #include <iostream>
    #include <ctime>
    
    int main() {
        // Unix timestamp representing the number of seconds since the Unix epoch
        time_t unixTimestamp = 1652642245;
    
        // Convert Unix timestamp to a tm structure (UTC)
        tm *dateTime = gmtime(&unixTimestamp);
    
        // Print the converted date and time
        std::cout << "Converted Date: "
                    << dateTime->tm_year + 1900 << "-"
                    << dateTime->tm_mon + 1 << "-"
                    << dateTime->tm_mday << " "
                    << dateTime->tm_hour << ":"
                    << dateTime->tm_min << ":"
                    << dateTime->tm_sec << std::endl;
    
        return 0;
    }          
        

Output:
Converted Date: 2022-05-15 19:17:25
Example only. There may be multiple ways to perform this operation.

Code Explanation

In this C++ example, the Unix timestamp '1652642245' is passed to the gmtime() function, which converts the timestamp to a tm structure representing the date and time in UTC.
The tm_year is the number of years since 1900, and the tm_mon is the month (0-based), so both are adjusted accordingly. The result is a human-readable date of '2022-05-15 19:17:25'. The gmtime() function ensures the timestamp is interpreted in UTC; you can use localtime() instead for local time conversions.



Other useful C++ date functions

Function Description Example
time() Returns the current Unix timestamp (seconds since January 1, 1970). time_t now = time(nullptr); returns 1693227296 (current timestamp)
localtime() Converts a Unix timestamp to a tm structure representing local time. localtime(&now) returns a tm structure for local time
gmtime() Converts a Unix timestamp to a tm structure representing UTC (Coordinated Universal Time). gmtime(&now) returns a tm structure for UTC time
mktime() Converts a tm structure representing local time into a Unix timestamp. time_t timestamp = mktime(&tm_struct); converts tm to a timestamp
strftime() Formats a tm structure into a string according to a format string. strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &tm_struct); returns a formatted string
difftime() Returns the difference in seconds between two Unix timestamps. double diff = difftime(time1, time2); returns the time difference in seconds
asctime() Converts a tm structure to a string representing the local date and time. asctime(&tm_struct) returns a string, e.g., Wed Aug 28 12:34:56 2024
ctime() Converts a Unix timestamp to a string representing the local date and time. ctime(&now) returns a string, e.g., Wed Aug 28 12:34:56 2024
std::chrono::system_clock::now() Returns the current time point as a std::chrono::time_point object. auto now = std::chrono::system_clock::now(); returns the current time
std::chrono::duration_cast() Converts one duration type to another (e.g., seconds to milliseconds). std::chrono::duration_cast(duration); converts duration
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