Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Leap Year 1

Status
Not open for further replies.

briancoats

Programmer
May 1, 2003
61
US
IS there a builtIn function to figure if a year is a leap year. I know how to calculate it, I am just an energy conservationist. OK, OK, so I am lazy

Brian Coats
 
You could try DateAdd on Feb 28 and see if you get March 1st
[smile]

If Month(DateAdd("d", 1, "28-Feb-95")) = 3 then
'not leap year




________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
the "general" rule is as follows:-

"Leap Years-These are calculated as follows : Every year divisible by 4 is a leap year. However, every year divisible by 100 is not a leap year. However, every year divisible by 400 is a leap year after all.

So, 1700, 1800, 1900, 2100, and 2200 are not leap years. But 1600, 2000, and 2400 are leap years."

but johnwms method is a lot easier to implement!!

enjoy!

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
A General Guide To Excel in VB FAQ222-3383
The Great Date Debate Thread222-368305
File Formats Galore @
 
oops... you knew that anyway... [lol]

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
A General Guide To Excel in VB FAQ222-3383
The Great Date Debate Thread222-368305
File Formats Galore @
 

Or similar to johnwm's idea:

?Day(DateSerial(Year(TheDate),2,29)) = 29
?Month(DateSerial(Year(TheDate),2,29)) = 2

Which uses the fast DateSerial function (almost three times faster than DateAdd(), once having to add the Year function as well).


The method I like the best, is to test if the 29th of Feb for that year is a valid date using the IsDate function:

TheYear = "2003"
?IsDate("02-29-" & TheYear)

Or

TheDate= Date()
?IsDate("02-29-" & Year(TheDate))

Even though this only needs to use 2 functions, verses the three needed for the DateAdd() method, and is also faster than using the DateAdd() method, it is still alot slower than using the DateSerial, even though that also uses 3 functions.

However, the speed difference is probably not a factor to worry about here at all, unless you are calling the function repeatedly in a loop, such as a recordset loop with alot of records, or used with-in an SQL statement using the Jet provider.
 
Thanks everybody. I guess I was hoping that there was a one function thing but I am going to use the IsDate function as I have used that one and would rather not use functions I don't know or understand. If you wouldn't mind, could you explain the parts of the dateSerial function so that I may be able to use that in the future.

Thanks again.

Brian
 
Just type DateSerial into the code window and press F1 on it.
 
don't forget the Julian/Gregorian calendar changeover. depends how far back in time you want to go. 1700 on is relatively safe. And the year dot - ISN'T. 1BC and 1AD are contiguous.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top