C++ Academy · Lesson

Calendar and Time Zones

Use C++20 calendar types.

Lesson 4 of 413 steps

Calendar and Time Zones is a free C++ Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.

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;
}

Building a Date

Combine the three pieces with the / operator to form a year_month_day. The order reads year, month, day.

#include <iostream>
#include <chrono>

int main() {
    using namespace std::chrono;
    year_month_day ymd = 2026y/May/30;
    std::cout << static_cast<int>(ymd.year()) << "-"
              << static_cast<unsigned>(ymd.month()) << "-"
              << static_cast<unsigned>(ymd.day()) << '\n';
    return 0;
}

Validating Dates

A year_month_day knows whether it is a real calendar date. Use ok() to reject impossible values like February 30.

#include <iostream>
#include <chrono>

int main() {
    using namespace std::chrono;
    year_month_day good = 2026y/February/28;
    year_month_day bad = 2026y/February/30;
    std::cout << std::boolalpha << good.ok() << " " << bad.ok() << '\n';
    return 0;
}

sys_days

Convert a date to sys_days, a count of days since the epoch. This lets you do day arithmetic and compute differences between dates.

#include <iostream>
#include <chrono>

int main() {
    using namespace std::chrono;
    sys_days a = sys_days{2026y/May/30};
    sys_days b = sys_days{2026y/May/20};
    std::cout << "Days apart: " << (a - b).count() << '\n';
    return 0;
}

Day Arithmetic

Adding days{n} to a sys_days moves forward by n days, then you can convert back to a calendar date.

#include <iostream>
#include <chrono>

int main() {
    using namespace std::chrono;
    sys_days start = sys_days{2026y/May/30};
    sys_days later = start + days{10};
    year_month_day ymd = later;
    std::cout << static_cast<unsigned>(ymd.day()) << "/"
              << static_cast<unsigned>(ymd.month()) << '\n';
    return 0;
}

Day of the Week

Convert a sys_days to a weekday to find which day of the week a date falls on.

#include <iostream>
#include <chrono>

int main() {
    using namespace std::chrono;
    weekday wd{sys_days{2026y/May/30}};
    std::cout << "Weekday index (Sun=0): " << wd.c_encoding() << '\n';
    return 0;
}

Last Day of a Month

The special last tag computes the final day of any month, handling leap years automatically.

#include <iostream>
#include <chrono>

int main() {
    using namespace std::chrono;
    year_month_day_last eom = 2024y/February/last;
    year_month_day ymd = eom;
    std::cout << "Feb 2024 ends on day " << static_cast<unsigned>(ymd.day()) << '\n';
    return 0;
}

Leap Year Check

The year type exposes is_leap() so you do not have to remember the divisibility rules yourself.

#include <iostream>
#include <chrono>

int main() {
    using namespace std::chrono;
    std::cout << std::boolalpha;
    std::cout << "2024 leap: " << year{2024}.is_leap() << '\n';
    std::cout << "2026 leap: " << year{2026}.is_leap() << '\n';
    return 0;
}

From a Time Point to a Date

Floor a system_clock time point to days and convert it to a year_month_day to get today's calendar date.

#include <iostream>
#include <chrono>

int main() {
    using namespace std::chrono;
    auto today = floor<days>(system_clock::now());
    year_month_day ymd = sys_days{today};
    std::cout << "Year is in range: " << (static_cast<int>(ymd.year()) > 2000) << '\n';
    return 0;
}

Time Zones

C++20 also adds time zones via std::chrono::zoned_time and the IANA database. Availability depends on your standard library, but the model wraps a time point with a named zone for correct local-time display.

#include <iostream>
#include <chrono>

int main() {
    using namespace std::chrono;
    auto now = system_clock::now();
    auto days_part = floor<days>(now);
    std::cout << "Have a UTC time point: " << (days_part.time_since_epoch().count() > 0) << '\n';
    return 0;
}

Quick Check

Test your understanding of C++20 calendar types.

Recap

You learned about C++20 calendar support:

  • year, month, day, and year_month_day with the / builder
  • ok() validation and is_leap() checks
  • sys_days for day arithmetic and weekday lookup
  • last for end-of-month, plus an introduction to zoned_time

That completes the Time and the Chrono Library course.

Free to start

Learn C++ with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Calendar and Time Zones” lesson free?

Yes — any 3 lessons of the C++ Academy learning path, including “Calendar and Time Zones”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.

What will I learn in “Calendar and Time Zones”?

Use C++20 calendar types. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

How long does the “Calendar and Time Zones” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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