Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Test for Leap Year

Total Hit ( 3305)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


As long as you trust VB's IsDate function (and you can, trust me), here's the shorted and fastest method to check if a year is a leap year or not:

Click here to copy the following block
Function IsLeapYear(year As Integer) As Boolean
 IsLeapYear = IsDate("2/29/" & CStr(year))
End Function

Here's is another quick method, based on the DateSerial function

Click here to copy the following block
IsLeapYear = DateSerial(year, 2, 29) <> DateSerial(year, 3, 1)

UPDATE: Mathias Schiffer from Germany sent us the "regular" code that lets you get to the same result without relying on the IsDate function. This is based on the definition of leap year, that is: a leap year must be divisible by 4 (without any remainder), and century years (eg 1800, 1900, 2000, ...) are leap years only if they're exactly divisble by 400. For example, 1900 wasn't a leap year, while 2000 is.
Function IsLeapYear(year As Integer) As Boolean
 IsLeapYear= (year Mod 4 = 0 And (year Mod 100 <> 0 Or year Mod 400 = 0))
End Function

UPDATE: Monte Hansen from Austria has pointed out that year 3600 is not a leap year. This correction has been proposed to account for the slowing of the Earth's spinning on it's axis and its slowing of its orbit around the sun - and to bring the seasonal, siderial and lunar years into synch again). So, the above function should be rewritten as:

Click here to copy the following block
Function IsLeapYear(year As Integer) As Boolean
 IsLeapYear= (year Mod 4 = 0 And (year Mod 100 <> 0 Or year Mod 400 = 0) And _
   year <> 3600)
End Function

For most practical reasons this correction is useless, but it's surely an interesting point. Interestingly, though, the DateSerial function reports that year 3600 is a leap year, so it seems that - after all - you can't completely trust VB!


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.