<script Language="c#" runat="server">
void Page_Load()
{
int currentYear = DateTime.Now.Year();
if (currentYear % 400 == 0) {
Message2.Text = ("This is a leap year");
}
else {
Message2.Text = ("This is not a leap year");
}
}
Currently I am getting a RunTime error. My goal is to test whether or not the current year, using DateTime.Now.Year() is a leap year or not. I think the issue is that I am not properly converting year to int? Please advise.
A year is a leap year if the following conditions are satisfied: The year is multiple of 400. The year is multiple of 4 and not multiple of 100.
The bad news, this year is not a leap year since it is only 2022, but the good news is the next leap year is 2024, only 2 years or about 730 and a half days away! From that point, the following leap years would be 2028, 2032 and 2036.
The complete list of leap years in the first half of the 21st century is therefore 2000, 2004, 2008, 2012, 2016, 2020, 2024, 2028, 2032, 2036, 2040, 2044, and 2048.
In each century, one out of every four years is divisible by 4. Of the century years, only 400, 800, and 1200 are divisible by 400, leaving 100, 200, 300, 500, 600, 700, 900, 1000, and 1100 that are not.
You can just use DateTime.IsLeapYear():
if (DateTime.IsLeapYear(year))
{
//do stuff
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With