C ES1en. net and SQL method of determining whether a given year is a leap year

  • 2020-11-25 07:28:57
  • OfStack

This article describes how to determine whether a given date is a leap year in c#, ES2en.net,sql. Share to everybody for everybody reference. Specific implementation methods are as follows:

The C# code is as follows:

public bool IsLeapYear(int year)
{
        if ((year < 1) || (year > 9999))
        {
            throw new ArgumentOutOfRangeException("year", " The year must be from 1 to 9999 Between the digital .");
        }
        if ((year % 4) != 0)
        {
            return false;
        }
        if ((year % 100) == 0)
        {
            return ((year % 400) == 0);
        }
        return true;
}

VB.NET:

Public Function IsLeapYear(year As Integer) As Boolean
        If (year < 1) OrElse (year > 9999) Then
            Throw New ArgumentOutOfRangeException("year", " The year must be from 1 to 9999 Between the digital .")
        End If
        If (year Mod 4) <> 0 Then
            Return False
        End If
        If (year Mod 100) = 0 Then
            Return ((year Mod 400) = 0)
        End If
        Return True
End Function

The sql code is as follows:
udf_DaysInMonth_Ver2
CREATE FUNCTION [dbo].[udf_DaysInMonth]
(
    @Date DATETIME
)
RETURNS INT
AS
BEGIN
RETURN CASE WHEN MONTH(@Date) IN (1,3,5,7,8,10,12) THEN 31
            WHEN MONTH(@Date) IN (4,6,9,11) THEN 30
            ELSE CASE WHEN (YEAR(@Date) % 4 = 0 AND YEAR(@Date) % 100 <> 0) OR (YEAR(@Date) % 400  = 0)
                      THEN 29
                      ELSE 28
                 END
            END
END

So I've written all three instances directly in code.

I hope this article has been helpful for your C#, ES24en.NET and SQL programming.


Related articles: