Visual Basic Helper Routines
Pure VB: Determining Leap Years
Posted:   Monday March 25, 2002
Updated:   Monday December 26, 2011
Applies to:   VB3, VB4-16, VB4-32, VB5, VB6
Developed with:   VB6, Windows NT4
OS restrictions:   None
Author:   Douglas Marquardt, Larry Serflaten


Pure VB: Methods to Determine the Last Day of the Month
Pure VB: Methods to Determine Number of Days in a Month
Pure VB: Determining Leap Years
VarMonthName: Retrieve Localized Month Names
VarWeekdayName: Retrieve Localized Weekday Names

In scouting the newsgroups I came across two simple non-API functions for determining whether a given year is a leap year. Provided by Douglas Marquardt and Larry Serflaten, they each take a different approach to arrive at the same conclusion. The choice is yours.  
 BAS Module Code

 Form Code
To a form, add a command button and the following code:

Option Explicit
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
Private Sub Command1_Click()

   Print 2000,
   Print IsLeapYearA(2000),
   Print IsLeapYearB(2000)
   Print 1999,
   Print IsLeapYearA(1999),
   Print IsLeapYearB(1999)
   Print 1998,
   Print IsLeapYearA(1998),
   Print IsLeapYearB(1998)
   Print 1997,
   Print IsLeapYearA(1997),
   Print IsLeapYearB(1997)
   Print 1996,
   Print IsLeapYearA(1996),
   Print IsLeapYearB(1996)
End Sub

Function IsLeapYearA(ByVal yr As Integer) As Boolean

  'Larry Serflaten
   If ((yr Mod 4) = 0) Then 
      IsLeapYearA = ((yr Mod 100) > 0) Or ((yr Mod 400) = 0)
   End If
End Function

Public Function IsLeapYearB(ByVal yr As Integer) As Boolean

  'Douglas Marquardt
   IsLeapYearB = Day(DateSerial(yr, 2, 29)) = 29
End Function


PayPal Link
Make payments with PayPal - it's fast, free and secure!


Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy


Hit Counter