How to get the no of days in a month using C#

Karthikanbarasan
Posted by Karthikanbarasan under C# category on | Points: 40 | Views : 3411
 private static int[] NoofdaysInMonth = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

public int getDaysInMonth(int intmonth, int intyear)
{
int intdays;

if (intmonth < 1 || intmonth > 12 || intyear < 1 || intyear > 9999)
{
return 0;
}
if (intmonth != 2) for february check
{
intdays = NoofdaysInMonth[intmonth];
}
else
{
intdays = NoofdaysInMonth[2] + CheckLeapYear(intyear);
}
return intdays;
}

public int CheckLeapYear(int intyear)
{
if (intyear % 4 == 0 && intyear % 100 != 0 || intyear % 400 == 0)
return 1; // It is a leap year
else
return 0; // Not a leap year
}

Comments or Responses

Login to post response