C++:
Comparing two dates

How to:

C++ makes life easy with the <chrono> header.

#include <iostream>
#include <chrono>
#include <ctime>

int main() {
    using namespace std::chrono;

    // Create system_clock time points
    system_clock::time_point today = system_clock::now();
    system_clock::time_point someDay = system_clock::now() - hours(24); // Yesterday

    // Convert to time_t for comparison
    time_t today_time_t = system_clock::to_time_t(today);
    time_t someDay_time_t = system_clock::to_time_t(someDay);

    if (today_time_t > someDay_time_t) {
        std::cout << "Today is after someDay.\n";
    } else if (today_time_t < someDay_time_t) {
        std::cout << "Today is before someDay.\n";
    } else {
        std::cout << "Dates are the same.\n";
    }

    return 0;
}

Sample output:

Today is after someDay.

Deep Dive:

Since C++11, <chrono> is where it’s at for date and time. Before that, you were likely wrestling with <ctime> and structs like tm. Not pretty.

Alternatives? Sure, there’s third-party libraries like Boost.DateTime. But why complicate when <chrono> is right there and evolving.

Implementation details to keep in your back pocket:

  • std::chrono deals with time points and durations.
  • system_clock measures real-world time.
  • time_point is a specific point in time (e.g., a date).
  • time_t is an arithmetic type, handy for comparisons.

See Also: