- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
DateTime.IsLeapYear() Method in C#
The DateTime.IsLeapYear() method in C# is used to check whether the specified year is a leap year. The return value is a boolean, with TRUE if the year is a leap year, else FALSE.
Syntax
Following is the syntax −
public static bool IsLeapYear (int y);
Above, y is the year to be checked, 2010, 2016, 2019, etc.
Example
Let us now see an example to implement the DateTime.IsLeapYear() method −
using System; public class Demo { public static void Main() { int year = 2019; Console.WriteLine("Year = "+year); if (DateTime.IsLeapYear(year)){ Console.WriteLine("Leap Year!"); } else { Console.WriteLine("Not a Leap Year!"); } } }
Output
This will produce the following output −
Year = 2019 Not a Leap Year!
Example
Let us now see another example to implement the DateTime.IsLeapYear() method. Here, we will add an out of range year −
using System; public class Demo { public static void Main() { int year = 101910; Console.WriteLine("Year = "+year); if (DateTime.IsLeapYear(year)){ Console.WriteLine("Leap Year!"); } else { Console.WriteLine("Not a Leap Year!"); } } }
Output
This will produce the following output i.e. error will get generated. The Stack Trace would print the same error as shown below −
Year = 101910 Run-time exception (line 11): Year must be between 1 and 9999. Parameter name: year Stack Trace: [System.ArgumentOutOfRangeException: Year must be between 1 and 9999. Parameter name: year] at System.DateTime.IsLeapYear(Int32 year) at Demo.Main() :line 11
Advertisements