Szerkesztés

Megosztás a következőn keresztül:


year_month class

Represents a month and year. The day isn't specified.

Syntax

class year_month; // C++20

Members

Name Description
Constructors Construct a year_month
year Returns the year.
month Returns the month.
ok Verify that the year and month values are in the valid range.
operator+= Add the specified number of months or years.
operator-= Subtract the specified number of months or years.

Non-members

Name Description
from_stream Parse a year_month from a stream using the specified format
operator+ Add months and/or years.
operator- Subtract months and/or years.
operator== Determine whether two year_month values are equal.
operator<=> Compare two year_month values. The >, >=, <=, <, != operators are synthesized by the compiler.
operator<< Output a year_month to a stream.

Requirements

Header: <chrono> (since C++20)

Namespace: std::chrono

Compiler Option: /std:c++latest

Constructors

Construct a year_month.

1) year_month() = default;
2) constexpr year_month(const year& y, const month& m) noexcept;

Parameters

y
The year value.

m
The month value.

Remarks

1) The default constructor doesn't initialize the year or month value.
2) Construct a year_month with the specified values.

For information about C++20 syntax to specify dates, see operator/

Example: Create a year_month

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

using namespace std::chrono;

int main()
{
    year_month ym{2021y / June};

    std::cout << ym;
    return 0;
}
2021/Jun

month

Get the month.

constexpr month month() const noexcept;

Return value

The month value.

year

Get the year.

constexpr year year() const noexcept;

Return value

The year.

ok

Check if the year and month value stored in this year_month are both in the valid range.

constexpr bool ok() const noexcept;

Return value

true if the year_month year and month values are in the valid range. Otherwise, false.

operator+=

Add months or years to this year_month.

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

Parameters

dm
The number of months to add.

dy
The number of years to add.

Return value

*this, which reflects the result of the addition.

Example: operator +=

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

using namespace std::chrono;

int main()
{
    year_month ym{2021y / June};

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

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

    std::cout << ym;
    
    return 0;
}
2021/Jun
2022/Aug

operator-=

Subtract months or years from this year_month.

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

Parameters

dm
The number of months to subtract.

dy
The number of years to subtract.

Return value

*this, which reflects the result of the subtraction.

Example: operator -=

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

using namespace std::chrono;

int main()
{
    year_month ym{2021y / June};

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

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

    std::cout << ym;
    
    return 0;
}
2021/Jun
2020/Apr

See also

<chrono>
year
year_month_day
year_month_day_last
year_month_weekday
year_month_weekday_last
operator/
Header Files Reference