# 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.
LVL 1
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Owner, Developing Solutions LLCCommented:
Why not use:

Public Function fnLeapYear(SomeYear) as Boolean

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

End Function
0
Owner, Developing Solutions LLCCommented:
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
Owner, Developing Solutions LLCCommented:
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
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.