avi123
December 7th, 2004, 12:50 AM
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
Kheun
December 7th, 2004, 02:35 AM
Here's one.
#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);
}
marten_range
December 7th, 2004, 02:49 AM
And another using boost::date_time:
#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;
}
See www.boost.org (seems like the site is down right now) for more information.
Hope this helps