Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get number of a month?

Status
Not open for further replies.

sulacco

Technical User
Nov 8, 2002
74
0
0
RU
Hi, People. Does anybody know how to get number of days in
a certain month of a certain year? Looked all the way in MSDN but failed to find such function.
Tx.
 
Code:
int days_in_month ( int month ) {
    static int days[] = { 31, 28, 31, /* fill in the rest */ };
    return days[month-1];
}

Then implement a leap year function which tells you whether February should be 28 or 29 days long.

--
 
thank you, I did it much compex and damn worse.
Btw, so I have to implement it on my own after all.
 
How about leap years (29 days in Feb;)?
Code:
inline bool isLeapYear(int y)
{
  return y%4 == 0 && (y%100 != 0 || y%400 == 0);
}

int day_in_month(int month, int year)
{
   int d;
   ... // as in Salem's post
   d = days...
   if (month == 2 && isLeapYear(year))
       ++d;
   return d;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top