Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check to see if year is leap year

Tags:

c#

datetime

<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.

like image 515
penmas Avatar asked Sep 28 '16 00:09

penmas


People also ask

How do you check if a year is leap year without using any operators?

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.

Is 2022 a leap year True or false?

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.

Is 2025 a leap year Yes or no?

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.

Is year 500 a leap year?

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.


1 Answers

You can just use DateTime.IsLeapYear():

if (DateTime.IsLeapYear(year)) 
{
   //do stuff
}
like image 138
smead Avatar answered Oct 04 '22 00:10

smead