Leap year

  • Thread starter Thread starter Guest
  • Start date Start date
Hi, Joe.
How can I determine whether a year is a leap year?

This is a common question, and many different solutions have been created to
solve it. Pick your favorite of any of the following techniques:

----------------------------------------------

A quick way to check for leap years is:

IsLeapYear = IsDate("2/29/" & YearToCheck)

where "YearToCheck" is a numeric value, the year in question.

This takes advantage of VB's liberal variable typecasting ability to create
a string from "2/29/" & YearToCheck, like "2/29/1999" and then checks to see
if it's a valid date.

----------------------------------------------

IsLeap = (Month(DateSerial(YearNum, 2,29))=2)

----------------------------------------------

IsLeap = (((YearNum Mod 4)=0) And _
((YearNum Mod 100)>0)) Or _
((YearNum Mod 400)=0)

----------------------------------------------

Public Function IsLeapYear(yearToTest As Integer) As Boolean
' Returns True if yearToTest is a leap year; False if not
IsLeapYear = (DateSerial(yearToTest, 3, 1) <> DateSerial(yearToTest, 2,
29))
End Function

----------------------------------------------

Public Function IsLeapYear(TestYear as Integer) as Boolean
IsLeapYear = (Day(DateSerial(testYear, 3, 0)) = 29)
End Function

----------------------------------------------

Public Function IsLeapYear(ByVal SomeValue As Variant) As Boolean
On Error GoTo LocalError
Dim intYear As Integer
'The trick here is make sure that we get an integer
'The 3 Golden rules are:
'True if it is divisible by 4
'False if it is divisible by 100
'TRUE if it is divisble by 400
If IsDate(SomeValue) Then
intYear = CInt(Year(SomeValue))
Else
'try and get an integer from the parse
'does not matter if we get an error
'because the error trap will catch it
intYear = CInt(SomeValue)
End If
If TypeName(intYear) = "Integer" Then
IsLeapYear = ((intYear Mod 4 = 0) And _
(intYear Mod 100 <> 0) Or (intYear Mod 400 = 0))
End If
Exit Function
LocalError:
IsLeapYear = False
End Function

HTH.

Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address, so that a message
will be forwarded to me.)
Beware to those who use munged addresses: known newsgroup E-mail harvesters
for spammers are (e-mail address removed) and (e-mail address removed)

- - -
When you see correct answers to your question posted in Microsoft's Online
Community, please sign in to the Community and mark these posts as "Answers,"
so that all may benefit by filtering on "Answered questions" and quickly
finding the right answers to similar questions. Remember that the first and
best answers are often given to those who have a history of rewarding the
contributors who have taken the time to answer questions correctly.
 
Great response Gunny. One thing that I try to avoid is implicit conversions
from strings to months and days. This suggests I would never use your first
check. The better checks are the ones that use the explicit typecasting.
 
How can I determine whether a year is a leap year?

IsALeapYear = (Month(DateSerial(YearNum,2,29))=2)
IsALeapYear = (Day(DateSerial(YearNum,3,0))=29)

IsALeapYear = (DatePart("y",DateSerial(YearNum,12,31))=366)

IsALeapYear = (YearNum MOD 4)=0 And _
(YearNum MOD 100)>0 And _
(YearNum MOD 400)=0

Hope that helps


Tim F
 
Or you could always use hardcode the rules, since their not changing soon.

Check out http://www.timeanddate.com/date/leapyear.html for the rules.

Rule #1
function isDivisibleByFour(intYear as integer) as Boolean
'Add code to test that intYear is a valid year
isDivisibleByFour = (intYear/4 - Int(intYear/4) = 0)
end function

Rule #2
function isDivisibleBy100(intYear as integer) as Boolean
'Add code to test that intYear is a valid year
isDivisibleBy100 = (intYear/100 - Int(intYear/100) = 0)
end function

Rule #3
function isDivisibleBy400(intYear as integer) as Boolean
'Add code to test that intYear is a valid year
isDivisibleBy400 = (intYear/400 - Int(intYear/400) = 0)
end function

Then its just a matter of writting a function that applies the rules
based on the conditions listed at the site provided. Essentially If..Then's

function isLeapYear(intYear as integer)
isLeapYear = False 'Assume its not a leap year
If isDivisibleBy4 = True then isLeapYear = True
If isDivisibleBy100 = True then isLeapYear = False
If isDivisibleBy100 = True AND isDivisibleBy400 = True then _
isLeapYear = True
end function

The code was written off the top of my head so there may be some syntax
issues. However the intYear/400 - Int(intYear/400) WILL give you any
decimal remainder thus proving if the year is evenly divisible.

David H
(Can you tell that I've got a Business Analyst background?)
 
Back
Top