Get whether a year is leap year
DateTime.IsLeapYear() method return a value which indicate whether a specified year is a leap year. this method exists in System namespace.
DateTime.IsLeapYear(year) method need to pass a parameter named 'year'. the 'year' parameter value data type is System.Int32 which represent a
4 digit year.
the method return value data type is System.Boolean. so its only return true or false. if the method return true then the specified year is a leap year. otherwise it is not a leap year. if the specified year is less than 1 and greater than 9999 then the method throw an exception named ArgumentOutOfRangeException.
the following asp.net c# example code demonstrate us how can we check whether a specified year is leap year or not programmatically at run time in an asp.net application.
the method return value data type is System.Boolean. so its only return true or false. if the method return true then the specified year is a leap year. otherwise it is not a leap year. if the specified year is less than 1 and greater than 9999 then the method throw an exception named ArgumentOutOfRangeException.
the following asp.net c# example code demonstrate us how can we check whether a specified year is leap year or not programmatically at run time in an asp.net application.
IsLeapYear.aspx
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, System.EventArgs e) {
if(!this.IsPostBack)
{
Label1.Font.Size = FontUnit.Larger;
Label1.ForeColor = System.Drawing.Color.HotPink;
Label1.Font.Bold = true;
Label1.Font.Italic = true;
Button1.Font.Bold = true;
Button1.ForeColor = System.Drawing.Color.DarkGreen;
Button1.Text = "Check IsLeapYear";
}
}
protected void Button1_Click(object sender, System.EventArgs e) {
int year = DateTime.Now.Year;
int anotherYear = DateTime.Now.AddYears(3).Year;
string checkYear = DateTime.IsLeapYear(year).ToString();
string checkAnotherYear = DateTime.IsLeapYear(anotherYear).ToString();
Label1.Text = "Year " + year;
Label1.Text += " Is Leap Year?: " + checkYear;
Label1.Text += "<br />Year " + anotherYear;
Label1.Text += " Is Leap Year?: " + checkAnotherYear;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net date time example: how to get whether a year is leap year (IsLeapYear) in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Teal">asp.net date time example: Check IsLeapYear</h2>
<asp:Label
ID="Label1"
runat="server"
>
</asp:Label>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
OnClick="Button1_Click"
/>
</div>
</form>
</body>
</html>

- How to get day from a DateTime
- How to get the current time of day
- How to get day of year
- How to get system DateTime
- How to add 1 month to a given DateTime object
- How to add a TimeSpan to a DateTime object
- How to get the AM PM value from a DateTime object
- How to check if a DateTime is between two given dates
- How to get the last day of month using a given date
- How to get the month name from month number