Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Beginner
Aug 8, 1997



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Leap Year Rules

One of the trickier parts of handling date values is dealing with leap years. Everyone knows that every fourth year is a leap year in which February has 29 days instead of the typical 28. What is less known is that there are two more rules to check to determine whether a given year is a leap year or not:

  • Years that are evenly divisible by 100 are not leap years;
  • Years that are evenly divisible by 400 are leap years.

With that in mind, I wrote this function to test whether any

given year is a leap year:

Function IsLeap (iYear As Integer) _
        As Integer

        'Set Default Value
        IsLeap = False

        'Check the 400 Year rule
        If (iYear Mod 400 = 0) Then
                IsLeap = True
                GoTo IsLeap_Exit
        End If

        'Check the 100 Year rule
        If (iYear Mod 100 = 0) Then
        IsLeap = False
        GoTo IsLeap_Exit
        End If

        'Check the 4 Year rule
        If (iYear Mod 4 = 0) Then
        IsLeap = True
        IsLeap = False
        End If


End Function

(Note: This is the VB3 version. For VB4 substitute a Boolean return value for the function.)

Joseph H.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date