Hello,
I was wondering if there is a function/class that returns the number of days in a month according to month & year
I mean, for example it should retrun for February 28/29 according to the year
Or should I write one of my own?
Thanks
Avi
Printable View
Hello,
I was wondering if there is a function/class that returns the number of days in a month according to month & year
I mean, for example it should retrun for February 28/29 according to the year
Or should I write one of my own?
Thanks
Avi
Here's one.
Code:#include <time.h>
int getDaysOfMonthYear(int month, int year)
{
tm currentMonth = {0, 0, 0, 1 /* first day of the month */, month-1 /* Zero-base */, year-1900 /* Time starts at 1900 */, 0, 0, 0};
tm nextMonth = {0, 0, 0, 1, month, year-1900, 0, 0, 0};
time_t diff = mktime(&nextMonth) - mktime(¤tMonth);
return diff/(60*60*24);
}
And another using boost::date_time:
See www.boost.org (seems like the site is down right now) for more information.Code:#include <cstddef>
#include <cstdlib>
#include "boost/date_time/gregorian/gregorian.hpp"
#include <iostream>
int main(int, char*)
{
// Prints last day of feb 2004
std::cout
<< boost::gregorian::gregorian_calendar::end_of_month_day(2004, 2)
<< std::endl;
return EXIT_SUCCESS;
}
Hope this helps