C #/VB. NET/SQL determines whether the specified year is a leap year code.

Source: Internet
Author: User

This article introduces the program code used in c #, vb.net, and SQL to determine whether the specified date is a leap year. If you need it, refer to this article.

The Code is as follows: Copy code

Public bool IsLeapYear (int year)
{
If (year <1) | (year> 9999 ))
{
Throw new ArgumentOutOfRangeException ("year", "the year must be a number from 1 to 9999 .");
}

If (year % 4 )! = 0)
{
Return false;
}

If (year % 100) = 0)
{
Return (year % 400) = 0 );
}
Return true;
}

VB. NET:

The Code is as follows: Copy code

Public Function IsLeapYear (year As Integer) As Boolean
If (year <1) OrElse (year> 9999) Then
Throw New ArgumentOutOfRangeException ("year", "the year must be a number between 1 and 9999 .")
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

SQL

The Code is as follows: Copy code

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

In this way, I will write all three instances in the form of code.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.