Link to home
Start Free TrialLog in
Avatar of mlcktmguy
mlcktmguyFlag for United States of America

asked on

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.
Avatar of Lee W, MVP
Lee W, MVP
Flag of United States of America image

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.
ASKER CERTIFIED SOLUTION
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Why not use:

Public Function fnLeapYear(SomeYear) as Boolean

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

End Function
day late, dollar short!  ;-(

Dale
Avatar of mlcktmguy

ASKER

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
It is:  year Mod 4

/gustav
This works and is exactly what I need.  Thanks
You are welcome!

/gustav
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.
You probably mean: ".., which was a leap year".

/gustav
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

Open in new window

Regards, Joe
gustav,

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

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

Jim.
Far simpler to use the code that Gustav posted that uses the DateSerial() function.
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