Working with Dates in C++: Overview of Modern Libraries
Working with dates and time is one of the key tasks in software development. For a long time, C++ lacked a convenient standard toolkit, which led to the use of C functions (ctime, localtime) or third-party solutions. However, with the arrival of the C++11 standard and subsequent versions, the situation changed dramatically. In this article, we will look at three main approaches to working with dates in C++: the standard std::chrono library, the date.h extension by Howard Hinnant, and the powerful Boost.DateTime framework.
1. Standard Library std::chrono (C++11 and later)
std::chrono is part of the C++ standard library, providing types for representing time points (time_point), durations (duration), and clocks (clock). It is ideal for measuring time intervals, working with timers, and high-precision delays. However, until C++20, it lacked convenient tools for working with calendar dates (day, month, year).
Example: measuring code execution time
#include <iostream>#include <chrono>#include <thread>
int main() { auto start = std::chrono::high_resolution_clock::now();
// Simulate work std::this_thread::sleep_for(std::chrono::milliseconds(500));
auto end = std::chrono::high_resolution_clock::now(); auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << "Execution time: " << duration.count() << " ms"; return 0;}With C++20, std::chrono gained calendar support: types year, month, day, year_month_day, and operators for working with them. Now you can write:
#include <chrono>#include <iostream>
int main() { using namespace std::chrono; auto today = floor<days>(system_clock::now()); year_month_day ymd{today}; std::cout << int(ymd.year()) << "-" << unsigned(ymd.month()) << "-" << unsigned(ymd.day()) << ""; return 0;}2. date.h Library (Howard Hinnant)
Before the release of C++20, developers actively used the date.h library — a single-file extension for std::chrono written by Howard Hinnant. It provides a full set of functions for working with dates: parsing, formatting, calculating the difference between dates, working with time zones, and even support for the Julian calendar.
Example: parsing a string into a date
#include "date.h"#include <iostream>#include <sstream>
int main() { std::istringstream ss("2025-04-07"); date::sys_days tp; ss >> date::parse("%F", tp); // ISO 8601 format auto ymd = date::year_month_day{tp}; std::cout << "Year: " << int(ymd.year()) << ""; return 0;}Example: calculating age
#include "date.h"#include <iostream>
int main() { using namespace date; auto birth = sys_days{year{1990}/month{1}/day{15}}; auto today = floor<days>(std::chrono::system_clock::now()); auto age = today - birth; std::cout << "Age in days: " << age.count() << ""; // Age in years (approximate) auto years = duration_cast<std::chrono::years>(age); std::cout << "Age in years: " << years.count() << ""; return 0;}The date.h library is easy to include (just copy the header file) and does not require building. It became the foundation for the standard std::chrono in C++2