Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleDetermine whether a year is a leap year with Visual Basic 6
DescriptionThis example shows how to determine whether a year is a leap year with Visual Basic 6.
Keywordsleap year, year, dates
CategoriesMiscellany, Strings
 
Thanks to WJK.

This example contains four functions that determine whether a year is a leap year. When the program starts, it uses each to see which years between 1880 and 2020 are leap years and adds them to four list boxes, one for each function. If you make the form tall enough, you can see that they all agree.

 
Public Function IsLeapYear0(ByVal y As Integer) As Boolean
    On Error Resume Next
    IsLeapYear0 = False
    'If the year is evenly divisible by 4 and not by 100,
    ' then this
    'is a leap year.
    If y Mod 4 = 0 And y Mod 100 <> 0 Then
        IsLeapYear0 = True
        'If the year is evenly divisible by 4 and 100, then
        ' check to
        'see if the quotient of year divided by 100 is also
        ' evenly
        'divisible by 4. If it is, then this is a leap year.
    ElseIf y Mod 4 = 0 And y Mod 100 = 0 Then
        If (y \ 100) Mod 4 = 0 Then IsLeapYear0 = True
    End If
End Function

Private Function IsLeapYear1(ByVal y As Integer) As Boolean
    If y Mod 4 <> 0 Then
        IsLeapYear1 = False
    ElseIf y Mod 400 = 0 Then
        IsLeapYear1 = True
    ElseIf y Mod 100 = 0 Then
        IsLeapYear1 = False
    Else
        IsLeapYear1 = True
    End If
End Function

Private Function IsLeapYear2(ByVal y As Integer) As Boolean
    IsLeapYear2 = _
        (y Mod 4 = 0) And _
        ((y Mod 100 <> 0) Or (y Mod 400 = 0))
End Function

Private Function IsLeapYear3(ByVal y As Integer) As Boolean
    IsLeapYear3 = IsDate("2/29/" & y)
End Function

Private Sub Form_Resize()
Dim hgt As Single
Dim i As Integer

    hgt = ScaleHeight - 2 * lstLeapYears(0).Top
    If hgt < 120 Then hgt = 120
    
    For i = 0 To 3
        lstLeapYears(i).Height = hgt
    Next i
End Sub
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated