C#或VB.NET判断指定的年份是否为闰年
老师布置的作业中,有涉及写一个函数是否为闰年。Insus.NET分别写了C#和VB.NET,以作备忘与参考。
C#:
public bool IsLeapYear(int year)
{
if ((year < 1) || (year > 9999))
{
throw new ArgumentOutOfRangeException("year", "年份必须是从1至9999之间数字.");
}
if ((year % 4) != 0)
{
return false;
}
if ((year % 100) == 0)
{
return ((year % 400) == 0);
}
return true;
}
{
if ((year < 1) || (year > 9999))
{
throw new ArgumentOutOfRangeException("year", "年份必须是从1至9999之间数字.");
}
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", "年份必须是从1至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
If (year < 1) OrElse (year > 9999) Then
Throw New ArgumentOutOfRangeException("year", "年份必须是从1至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 Server 闰年相关可参考:
http://www.cnblogs.com/insus/articles/2025019.html
http://www.cnblogs.com/insus/archive/2011/09/10/2173028.html