month
class
Represents a month of a year. For example, July.
class month; // C++20
A month
normally holds values in the range [1, 12]. It can also hold non-negative values outside this range.
See Month constants, below, for constants that you can use with the month
class.
Name | Description |
---|---|
Constructors | Construct a month . |
ok |
Verify that the month value is in the valid range [1,12]. |
operator++ |
Increment the month . |
operator+= |
Add the specified number of months to this month . |
operator-- |
Decrement this month . |
operator-= |
Subtract the specified number of months from this month . |
operator unsigned |
Get the month value. |
Name | Description |
---|---|
from_stream |
Parse a month from the given stream using the specified format. |
operator+ |
Add specified number of months to this month , returning a new month instance. |
operator- |
Subtract the specified number of months from this month. Returns a new month instance. |
operator== |
Determine whether two months are equal. |
operator<=> |
Compare this month against another month. The >, >=, <=, <, != operators are synthesized by the compiler. |
operator<< |
Output a month to the given stream. |
Header: <chrono>
(since C++20)
Namespace: std::chrono
Compiler Option: /std:c++latest
Construct a month
.
1) month() = default;
2) explicit constexpr month(unsigned m) noexcept;
m
Construct a month
with value m
.
1) The default constructor doesn't initialize the day value.
2) Construct a month
with the day value initialized to m
.
Check if the value stored in this month
is in the valid range.
constexpr bool ok() const noexcept;
true
if the month value is in the range [1,12]. Otherwise, false
.
Increment the month value.
1) constexpr month& operator++() noexcept;
2) constexpr month operator++(int) noexcept;
1) A reference to *this
month after it has been incremented (a postfix increment).
2) A copy of the month
, before it has been incremented (a prefix increment).
// compile using: /std:c++latest
#include <iostream>
#include <chrono>
using namespace std::chrono;
int main()
{
month m{ January };
month m2{4}; // April
std::cout << m << " " << ++m << "\n"; // constexpr month& operator++() noexcept
std::cout << m << " " << m++ << "\n"; // constexpr month operator++(int) noexcept
std::cout << m << "\n";
std::cout << m2 << "\n";
return 0;
}
Jan Feb
Feb Feb
Mar
Apr
If the result exceeds 12, it's set to 1.
Subtract 1 from the month value.
1) constexpr month& operator--() noexcept;
2) constexpr month operator--(int) noexcept;
1) A reference to *this
month
after it has been decremented (a postfix decrement).
2) A copy of the month
before it has been decremented (a prefix decrement).
// compile using: /std:c++latest
#include <iostream>
#include <chrono>
using namespace std;
using namespace std::chrono;
int main()
{
month m{May};
cout << m << " " << --m << "\n"; // constexpr month& operator++() noexcept
cout << m << " " << m-- << "\n"; // constexpr month operator++(int) noexcept
cout << m << "\n";
return 0;
}
May Apr
Apr Apr
Mar
If the decremented result is less than 1, it's set to 12.
Add months to this month
.
constexpr month& operator+=(const months& m) noexcept;
m
The number of months to add.
*this
Subtract months
from this month
.
constexpr month& operator-=(const months& m) noexcept;
m
The months to subtract.
*this
.
Get the unsigned month
value.
explicit constexpr operator unsigned() const noexcept;
The unsigned value of this month
// compile using: /std:c++latest
#include <iostream>
#include <chrono>
using namespace std::chrono;
int main()
{
month m{July};
unsigned monthValue = static_cast<unsigned>(m);
std::cout << monthValue << "\n";
return 0;
}
7
(C++20) The <chrono>
header defines the following constants that you can use with month
for greater convenience, type-safety, and maintainability of your code. These constants are in scope when std::chrono
is in scope.
// Calendrical constants
inline constexpr month January{1};
inline constexpr month February{2};
inline constexpr month March{3};
inline constexpr month April{4};
inline constexpr month May{5};
inline constexpr month June{6};
inline constexpr month July{7};
inline constexpr month August{8};
inline constexpr month September{9};
inline constexpr month October{10};
inline constexpr month November{11};
inline constexpr month December{12};
<chrono>
month_day
Class
month_day_last
Class
month_weekday
Class
month_weekday_last
class