How to check if the year is Leap Year or not in C#

Karthikanbarasan
Posted by Karthikanbarasan under C# category on | Points: 40 | Views : 21395
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

Posted by: T.saravanan on: 2/2/2011 Level:Silver | Status: [Member] [MVP] | Points: 10
Hi Karthik,

Using DateTime keyword we can easily find out the year is leap (or) not.
int nYear = 2005;

bool bResult = DateTime.IsLeapYear(nYear);
if(bResult.Equals(true))
{
// Its Leap Year
}
else
{
// Its not a Leap Year
}


Cheers :)

Login to post response