0Pricing
C++ Academy · Lesson

Calendar and Time Zones

Use C++20 calendar types.

C++20 Calendar Types

C++20 added rich calendar support to <chrono>. You can now express years, months, and days directly as types rather than juggling integers.

  • year, month, day
  • year_month_day for a full date
#include <iostream>
#include <chrono>

int main() {
    using namespace std::chrono;
    year y{2026};
    std::cout << "Year value: " << static_cast<int>(y) << '\n';
    return 0;
}

Calendar Literals

Calendar literals like 2026y and 15d plus named months such as std::chrono::May make dates read naturally.

#include <iostream>
#include <chrono>

int main() {
    using namespace std::chrono;
    auto y = 2026y;
    auto d = 15d;
    std::cout << static_cast<int>(y) << " / " << static_cast<unsigned>(d) << '\n';
    return 0;
}

All lessons in this course

  1. Durations and Clocks
  2. Time Points
  3. Measuring Code Performance
  4. Calendar and Time Zones
← Back to C++ Academy