xtd 0.2.0
Loading...
Searching...
No Matches

◆ days_in_month() [2/2]

static int32 xtd::date_time::days_in_month ( uint32  year,
uint32  month 
)
static

Returns the number of days in the specified month and year.

Parameters
yearThe year.
monthThe month (a number ranging from 1 to 12).
Returns
The number of days in month for the specified year.
For example, if month equals 2 for February, the return value is 28 or 29 depending upon whether year is a leap year.
Exceptions
xtd::argument_out_of_range_exceptionmonth is less than 1 or greater than 12.
-or-
year is less than 1 or greater than 9999.
Examples
The following example demonstrates how to use the xtd::date_time::days_in_month method to determine the number of days in July 2001, February 1998 (a non-leap year), and February 1996 (a leap year).
#include <xtd/console>
#include <xtd/date_time>
#include <xtd/startup>
class program {
public:
static auto main() {
xtd::console::write_line(days_in_july);
// days_in_feb gets 28 because the year 1998 was not a leap year.
// days_in_feb_leap gets 29 because the year 1996 was a leap year.
xtd::console::write_line(days_in_feb_leap);
}
};
startup_(program::main);
// This code produces the following output :
//
// 31
// 28
// 29
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
static int32 days_in_month(uint32 year, month_of_year month)
Returns the number of days in the specified month and year.
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.h:175
@ july
Indicates july.
@ february
Indicates february.
Examples
The following example displays the number of days in each month of a year specified in an integer array.
#include <xtd/as>
#include <xtd/console>
#include <xtd/date_time>
#include <xtd/startup>
#include <array>
using namespace xtd;
class program {
public:
static auto main() {
auto years = std::array<unsigned int, 2> {2012, 2014};
console::write_line("Days in the Month for the Gregorian calendar\n");
console::write_line("{0,-10}{1,-15}{2,4}\n", "Year", "Month", "Days");
for (auto year : years) {
for (auto ctr = 1u; ctr <= 12u; ctr++)
console::write_line("{0,-10}{1,-15}{2,4}", year, as<month_of_year>(ctr), date_time::days_in_month(year, ctr));
console::write_line();
}
}
};
startup_(program::main);
// This code produces the following output :
//
// Days in the Month for the en-US culture using the Gregorian calendar
//
// Year Month Days
//
// 2012 january 31
// 2012 february 29
// 2012 march 31
// 2012 april 30
// 2012 may 31
// 2012 june 30
// 2012 july 31
// 2012 august 31
// 2012 september 30
// 2012 october 31
// 2012 november 30
// 2012 december 31
//
// 2014 january 31
// 2014 february 28
// 2014 march 31
// 2014 april 30
// 2014 may 31
// 2014 june 30
// 2014 july 31
// 2014 august 31
// 2014 september 30
// 2014 october 31
// 2014 november 30
// 2014 december 31
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
Remarks
The xtd::date_time::days_in_month method always interprets month and year as the month and year of the Gregorian calendar.