|
-
December 7th, 2004, 01:50 AM
#1
number of days in each month
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
-
December 7th, 2004, 03:35 AM
#2
Re: number of days in each month
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);
}
Last edited by Kheun; December 7th, 2004 at 03:38 AM.
-
December 7th, 2004, 03:49 AM
#3
Re: number of days in each month
And another using boost: ate_time:
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;
}
See www.boost.org (seems like the site is down right now) for more information.
Hope this helps
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|