std::chrono::operator+, std::chrono::operator- (std::chrono::year_month_day)

From cppreference.com
< cpp‎ | chrono‎ | year month day
Date and time library
Time point
(C++11)
(C++20)
Duration
(C++11)
Clocks
(C++11)
(C++11)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
Time of day
(C++20)(C++20)
(C++20)(C++20)
(C++20)

Calendar
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)
Time zone
(C++20)
(C++20)
(C++20) (C++20) (C++20) (C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
chrono I/O
(C++20)
C-style date and time
Defined in header <chrono>
constexpr std::chrono::year_month_day operator+ ( const std::chrono::year_month_day & ymd,

const std::chrono::months & dm

) noexcept ;
(since C++20)
constexpr std::chrono::year_month_day operator+ ( const std::chrono::months & dm,

const std::chrono::year_month_day & ymd

) noexcept ;
(since C++20)
constexpr std::chrono::year_month_day operator+ ( const std::chrono::year_month_day & ymd,

const std::chrono::years & dy

) noexcept ;
(since C++20)
constexpr std::chrono::year_month_day operator+ ( const std::chrono::years & dy,

const std::chrono::year_month_day & ymd

) noexcept ;
(since C++20)
constexpr std::chrono::year_month_day operator- ( const std::chrono::year_month_day & ymd,

const std::chrono::months & dm

) noexcept ;
(since C++20)
constexpr std::chrono::year_month_day operator- ( const std::chrono::year_month_day & ymd,

const std::chrono::years & dy

) noexcept ;
(since C++20)
1,2) Adds dm.count() months to the date represented by ymd. The result has the same day() as ymd and the same year() and month() as std::chrono::year_month (ymd.year ( ), ymd.month ( ) ) +
3,4) Adds dy.count() years to the date represented by ymd. The result is equivalent to std::chrono::year_month_day (ymd.year ( ) + dy, ymd.month ( ), ymd.day ( )
5) Subtracts dm.count() months from the date represented by ymd. Equivalent to ymd + -dm
6) Subtracts dy.count() years from the date represented by ymd. Equivalent to ymd + -dy

For durations that are convertible to both std::chrono::years and std::chrono::months, the years overloads (3,4,6)

Notes

Even if ymd.ok() is true, the resulting year_month_day may not represent a valid date if ymd.day()

Example

#include <chrono>
#include <iostream>
 
int main()
{
    std::cout << std::boolalpha;
 
    auto ymd{std::chrono::day(1)/std::chrono::July/2021};
 
    ymd = ymd + std::chrono::months(4);
    std::cout << (ymd.month() == std::chrono::November) << ' '
              << (ymd.year() == std::chrono::year(2021)) << ' ';
 
    ymd = ymd - std::chrono::years(10);
    std::cout << (ymd.month() == std::chrono::month(11)) << ' '
              << (ymd.year() == std::chrono::year(2011)) << '\n';
}

Output:

true true true true