How to check the current year is leap year or not using ASP.NET
Here I am going to explain you
how to check current year is leap year or not using the
method IsLeapYear. It will be usefull when you have to do some kind of
validation your code based on the year.
This is
one of the common question we see in .NET forum, people go
for SQL query or devide the year by zero and all to find the
leap year. But Microsoft has got an inbuilt method to find
out this.
IsLeapYear method takes one argument of
integer type so you need to pass the year to check whether
it is leap year or not.
Check below lines of
code,
protected void Page_Load(object sender, EventArgs
e)
{
Int32 DtYear =
DateTime.Now.Year;
if
(DateTime.IsLeapYear(DtYear))
{
Response.Write("This year is a Leap year!!!");
}
else
{
Response.Write("This year is Not a leap year!!!");
}
}
Method 2 without using IsLeapYear,
int year = 2012;
if ((year % 4
== 0 && year % 100 != 0) || year % 400 == 0)
{
Response.Write("Yes this is a Leap
Year!!!");
}
else
{
Response.Write("No, Year is Not a
Leap Year!!!");
}