c# - How to check whether the given year is a leap year

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.

<%@ 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) {
            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;

<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>
    <form id="form1" runat="server">
        <h2 style="color:Teal">asp.net date time example: Check IsLeapYear</h2>
        <br /><br />
More c# examples