• Status: Solved
• Priority: Medium
• Security: Public
• Views: 122

# Leap Year Identification

I am converting data from a legacy system that had some poor date editing.  I am finding issues in the dates, most of which are solved.

One issue is the number of days in a month.  For example they have a date of 11/31/2005, which throws an error when trying to convert to a standard date in Access.  I added logic to identify legacy dates with more than the standard days in a month.  It works for all months except February, in which case leap years have 29 days, non-leap years have 28.  I know I could test for leap years by entering all leap years.

Before doing that I was wondering if anyone has a routine that if passed a numeric year like 2012, can identify it as a leap year or not.
0
mlcktmguy
• 4
• 3
• 2
• +3
1 Solution

If year / mod 4 = 0 Then Leapyear = True

Without more information I'm not sure what you're looking for - leap years are every 4 years.  MOD returns 0 when it's divisible evenly. It won't work for the year 1700,1800,1900, 2100, 2200, 2300 (those aren't leap years) but all others are.
0

CIOCommented:
Yes:
``````Public Function IsLeapYear( _
Optional ByVal intYear As Integer) _
As Boolean

If intYear = 0 Then
intYear = Year(Date)
End If
IsLeapYear = Day(DateSerial(intYear, 2, 29)) = 29

End Function
``````
/gustav
0

Commented:
Why not use:

Public Function fnLeapYear(SomeYear) as Boolean

fnLeapYear = (Day(DateSerial([EnterYear], 3, 0)) = 29)

End Function
0

Commented:
day late, dollar short!  ;-(

Dale
0

Author Commented:
Thanks you and that should work for what I need.

However, I'm getting an 'Expected Expression' error with 'mod' highlighted when I try your statement
0

CIOCommented:
It is:  year Mod 4

/gustav
0

Author Commented:
This works and is exactly what I need.  Thanks
0

CIOCommented:
You are welcome!

/gustav
0

President / OwnerCommented:
For future readers, year mod 4 doesn't cut it.  There are three rules to determine a leap year:

1. Leap year if divisible by 4.

2. Not a leap year if divisible by 100.

3. Is a leap year if it is divisible by 400.

Those are applied in order.  They are a result of the fact that the year not exactly a number of whole days, (you end up with something like .23xxx if I remember right), so every four years, you end up with an extra day, but that's too much of and adjustment.

After 100 years, that over adjustment adds up to over a day, so no leap year.

But then after 400 years, you've gone too far the other way and need the leap year back.

The year that will give you problems with current software and year mod 4 is 2000, which was not a leap year.

Jim.
0

CIOCommented:
You probably mean: ".., which was a leap year".

/gustav
0

DeveloperCommented:
Here's a generic code snippet that does it for you. I've used this in several languages. Code it in whatever language you prefer. The "mod" is the modulo operation — the remainder in division.
``````LeapYear:=False
YearMod4:=mod(InputYear,4)
YearMod100:=mod(InputYear,100)
YearMod400:=mod(InputYear,400)
If (YearMod4=0) and (YearMod100<>0)
LeapYear:=True
If (YearMod400=0)
LeapYear:=True
``````
Regards, Joe
0

President / OwnerCommented:
gustav,

<<You probably mean: ".., which was a leap year".>

Yes your right...I was thinking of 1900, not 2000 that was a problem.

Jim.
0

Commented:
Far simpler to use the code that Gustav posted that uses the DateSerial() function.
0

DeveloperCommented:
Oops...I see this was closed while my browser tab had the original question. Jim is spot-on, other than the year 2000 comment, where Gustav is correct. Regards, Joe
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.