std::chrono::year_month::year, std::chrono::year_month::month

< cpp‎ | chrono‎ | year month
constexpr std::chrono::year year() const noexcept;
(1) (C++20 起)
constexpr std::chrono::month month() const noexcept;
(2) (C++20 起)

取得存储于此 year_month 对象的 yearmonth 值。

返回值

1) 返回存储的 std::chrono::year 值。
2) 返回存储的 std::chrono::month 值。

示例

#include <iostream>
#include <chrono>
 
int main()
{
    std::cout << std::boolalpha;
 
    constexpr auto ym {std::chrono::year(2021)/std::chrono::July};  
    std::cout << (ym.year() == std::chrono::year(2021)) << ' ';
    std::cout << (ym.month() == std::chrono::month(7)) << '\n';
 
}

输出:

true true