共用方式為


year_month_day 類別

表示月份、年和日。

語法

class year_month_day; // C++20

成員

名稱 描述
建構函式 建構 year_month_day
day 傳回日期。
month 傳回月份。
ok 確認 yearmonth 值在有效範圍內。
operator+= 新增指定的月數或年份。
operator-= 減去指定的月數或年份。
operator local_days 取得從 system_clock epoch 到這個 year_month_day 的天數計數。 local_days
operator sys_days 取得從 system_clock epoch 到這個 year_month_day 的天數計數。 sys_days
year 傳回年份。

非成員

名稱 描述
from_stream year_month_day使用指定的格式從數據流剖析
operator+ 新增月份或年份。
operator- 減去月份或年份。
operator== 判斷兩個 year_month_day 值是否相等。
operator<=> 比較兩個 year_month_day 值。 運算子 >, >=, <=, <, != 是由編譯程式合成。
operator<< year_month_day將輸出至數據流。

需求

標頭: <chrono> (自C++20起)

命名空間std::chrono

編譯程序選項: /std:c++latest

建構函式

year_month_day建構 。

1) year_month_day() = default;
2) constexpr year_month_day(const year& y, const month& m, day& d) noexcept;
3) constexpr year_month_day(const year_month_day_last& ymdl) noexcept;
4) constexpr year_month_day(const sys_days& dp) noexcept;
5) constexpr explicit year_month_day(const local_days& dp) noexcept;

參數

dday

dpsys_dayslocal_days 值。

mmonth

y
year 值。

ymdlyear_month_day_last

備註

1) 預設建構函式不會初始化月份或日。
2) 使用指定的年份、月和日建構 year_month_day
3) 使用指定的年份、月和日建構year_month_dayymdl
4) 建構 year_month_day與 相同日期 dp的 。
5) 使用與 相同的日期dp建構 ,year_month_day但如同 由 建構一year_month_day(sys_days(dp.time_since_epoch()))樣。

如需用來指定日期之C++20 語法的相關信息,請參閱 operator/

範例:建立 year_month_day

// compile using: /std:c++latest
#include <iostream>
#include <chrono>

using namespace std::chrono;

int main()
{
    year_month_day ymd{ April / 4 / 1975 };
    std::cout << ymd;
    return 0;
}
1975-04-04

day

取得一天。

constexpr day day() const noexcept;

傳回值

day 值。

month

取得月份。

constexpr month month() const noexcept;

傳回值

month 值。

operator local_days

取得從 system_clock epoch (1/1/1/1970) 到這個 year_month_day 的天數計數 local_days

constexpr explicit operator local_days() const noexcept;

傳回值

如果 ok()為 ,則傳回天數的計數為 local_days{sys_days{*this}.time_since_epoch()}

operator sys_days

取得從 system_clock epoch (1/1/1/1970) 到這個year_month_day的天數計數。sys_days

constexpr operator sys_days() const noexcept;

傳回值

如果ok()為 ,則會傳sys_days回從 Epoch (1/1/1/1970) 到在此year_month_day中保留的日期的天數sys_days。 如果這個 year_month_day 中的日期在 epoch 之前 sys_days ,此值將會是負值。

如果這個 year_month_day 中的年份和月份為 ok(),則會傳 sys_days{year/month/1d} + (day-1d)回 。 否則,未指定傳回的值。

sys_days範圍 [days{-12687428}days{11248737}] 中的 可以轉換成 year_month_day 和 返回,而且具有相同的值。

year

取得年份。

constexpr year year() const noexcept;

傳回值

year

ok

檢查儲存在此 中的 year_month_day 年份和月份值是否都位於有效範圍內。 確保日期在 [1d, (y/m/last)day()] 範圍內,佔閏年和每個月的不同天數。

constexpr bool ok() const noexcept;

傳回值

trueyear_month_day如果年份、月和日值在有效範圍內,則為 。 否則為 false

operator+=

將月份或年份新增至此 year_month_day

1) constexpr year_month_day& operator+=(const months& dm) noexcept;
2) constexpr year_month_day& operator+=(const years& dy) noexcept;

參數

dm
要加入的月數。

dy
要加入的年數。

傳回值

*this,反映加法的結果。

範例: operator+=

// compile using: /std:c++latest
#include <iostream>
#include <chrono>

using namespace std::chrono;

int main()
{
    year_month_day ymd{June / 1d / 2021y};

    std::cout << ymd << '\n';

    ymd += months{2};
    ymd += years{1};

    std::cout << ymd;
    
    return 0;
}
2021-06-01
2022-08-01

operator-=

從這個 year_month_day減去月份或年份。

1) constexpr year_month_day& operator-=(const months& dm) noexcept;
2) constexpr year_month_day& operator-=(const years& dy) noexcept;

參數

dm
要減去的月數。

dy
要減去的年數。

傳回值

*this,反映減法的結果。

範例: operator-=

// compile using: /std:c++latest
#include <iostream>
#include <chrono>

using namespace std::chrono;

int main()
{
    year_month_day ymd{June / 1d / 2021y};

    std::cout << ymd << '\n';

    ymd -= months{2};
    ymd -= years{1};

    std::cout << ymd;
    
    return 0;
}
2021-06-01
2020-04-01

另請參閱

<chrono>
year
year_month
year_month_day_last
year_month_weekday
year_month_weekday_last
operator/
標頭檔參考