Current Unix Timestamp SECONDS SINCE JAN 01 1970

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

How to convert Datetime to Unix timestamp in C++

🎈 🎈 🎈
+1

    #include <iostream>
    #include <ctime>
    
    int main() {
        // Define the date and time (e.g., 2022-05-15 09:50:45)
        tm dateTime = {};
        dateTime.tm_year = 2022 - 1900;  // Year since 1900
        dateTime.tm_mon = 5 - 1;         // Month (0-based, so May is 4)
        dateTime.tm_mday = 15;           // Day of the month
        dateTime.tm_hour = 9;
        dateTime.tm_min = 50;
        dateTime.tm_sec = 45;
    
        // Convert the tm structure to a Unix timestamp
        time_t unixTimestamp = mktime(&dateTime);
    
        // Print the Unix timestamp
        std::cout << "Unix Timestamp: " << unixTimestamp << std::endl;
    
        return 0;
    }
        

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

Code Explanation

In this C++ example, the tm structure is initialized to represent the date 2022-05-15 08:10:45. The fields of the tm structure are filled in manually: tm_year is the number of years since 1900, and tm_mon is 0-based, so they are adjusted accordingly.
The mktime() function then converts the tm structure into a Unix timestamp, representing the number of seconds since the Unix epoch (January 1, 1970). The resulting Unix timestamp is 1652602245, which corresponds to the specified date and time.



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