In C#, DateTime.IsLeapYear(year) returns true when a year has 366 days. In a leap year February has 29 days.
The rule is built in: a year is a leap year if it is divisible by 4, except century years, which must also be divisible by 400.
This is a static member of System.DateTime. You call it with the type name, such as DateTime.IsLeapYear(...). DateTime holds a date and time of day with a Kind of Utc, Local, or Unspecified.
Syntax
Call it with the type name and pass a four-digit year.
C#
bool a = DateTime.IsLeapYear(2024); // true
bool b = DateTime.IsLeapYear(2026); // false
bool c = DateTime.IsLeapYear(1900); // false (century rule)
Call
What it does
DateTime.IsLeapYear(2024)
True: divisible by 4.
DateTime.IsLeapYear(1900)
False: century year not divisible by 400.
DateTime.IsLeapYear(2000)
True: divisible by 400.
The year must be between 1 and 9999. Other values throw ArgumentOutOfRangeException.
Library declarations
C#
public static bool IsLeapYear(int year);
Parameters
Parameter
Description
year
Required. A year from 1 to 9999.
Return Value
Return Value
Description
bool
true if the year is a leap year; otherwise false.
C# Examples
Example 1 — Check Three Years
Print the result for 2024, 2026, and 2028.
C#
using System;
class Program {
static void Main() {
Console.WriteLine("2024: " + DateTime.IsLeapYear(2024));
Console.WriteLine("2026: " + DateTime.IsLeapYear(2026));
Console.WriteLine("2028: " + DateTime.IsLeapYear(2028));
}
}
Output
2024: True
2026: False
2028: True
How It Works
String plus bool. C# turns the bool into the text True or False.
2024 and 2028. Both are divisible by 4, so they are leap years.
2026. It leaves a remainder of 2 when divided by 4, so it is a normal year.
Four years apart. Leap years usually come every four years.
Example 2 — 1900, 2000, and 2100
Test the exceptions to the divisible-by-4 rule.
C#
using System;
class Program {
static void Main() {
foreach (int year in new[] { 1900, 2000, 2100 }) {
Console.WriteLine(year + " -> " + DateTime.IsLeapYear(year));
}
}
}
Output
1900 -> False
2000 -> True
2100 -> False
How It Works
Array of years. The loop tests three century years in a row.
1900 and 2100. They are divisible by 100 but not by 400, so they are not leap years.
2000. It is divisible by 400, so it is a leap year.
Arrow text. Each line joins the year, an arrow, and the bool result.
Example 3 — Find the Next Leap Year
Loop from 2026 until IsLeapYear is true, then handle an invalid year.
C#
using System;
class Program {
static void Main() {
int year = 2026;
while (!DateTime.IsLeapYear(year)) year++;
Console.WriteLine("Next leap year: " + year);
Console.WriteLine("February days: " + DateTime.DaysInMonth(year, 2));
try {
DateTime.IsLeapYear(10000);
} catch (ArgumentOutOfRangeException) {
Console.WriteLine("Year must be 1-9999");
}
}
}
Output
Next leap year: 2028
February days: 29
Year must be 1-9999
How It Works
Start at 2026. The loop checks one year at a time.
2027 is skipped. The loop stops at 2028, the first year where the test is true.
Cross-check.DaysInMonth(2028, 2) returns 29, which agrees with the leap-year result.
Invalid year. 10000 is above the maximum of 9999, so the exception is caught and a message is printed.
Frequently Asked Questions
Yes. 2028 is divisible by 4 and is not a century year.
Century years must be divisible by 400. 1900 is not, so it is not a leap year.
Not safely. It gives the wrong answer for 1900 and 2100. Use DateTime.IsLeapYear.
It throws ArgumentOutOfRangeException. Valid years are 1 to 9999.