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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

leap year 2

Status
Not open for further replies.

skate

Technical User
Nov 29, 2002
65
0
0
CA
is there a function that tells me is a certain date inputed is a leap year?
 
How about: If month(txtDate) = 2 and and day(txtDate) = 29?

What are you trying to do?
 
Leap years are a little complicated but not impossible.

DEFINITION:
According to the Gregorian calendar, which is the civil calendar in use today, years evenly divisible by 4 are leap years, with the exception of centurial years that are not evenly divisible by 400. Therefore, the years 1700, 1800, 1900 and 2100 are not leap years, but 1600, 2000, and 2400 are leap years.

The following equation implements the above definition:

Dim vDate As Date
vDate = CDate(InputBox("Enter Date:"))
Me![LeapYear] = IIf(DatePart("yyyy", vDate) Mod 4 = 0 And DatePart("yyyy", vDate) Mod 400 = 0, True, False)
I created a form with a Control called LeapYear. The Format for the control is True/False. The above code behind a button will let you see how the equation works. Enter your date in mm/dd/yyyy format when prompted. The result of the evaluation will be displayed in the control as True or False.

Hope this helps you with your project.

Bob Scriver
 
In addition to the answer above here is a Function as you requested. Just copy and Paste into a database class module:

Function LeapYear(vDate As Date) As Boolean
LeapYear = IIf(DatePart("yyyy", vDate) Mod 4 = 0 And DatePart("yyyy", vDate) Mod 400 = 0, True, False)
End Function


Just make a call to this function like this:

Dim vLeapYear as boolean
vLeapYear = LeapYear(Me![YourDate])


This should work as you requested. Bob Scriver
 
Why not just 'trust' MS? I haven't seen them fail to include Feb 29 when it is supposed to be there, and this ALWAYS affects the # of days in the year:

Public Function basIsLeapYr(DtIn as Date) as Boolean

Code:
Public Function basIsLeapYr(DtIn As Date) As Boolean

    basIsLeapYr = (DateDiff("d", DateSerial(Year(DtIn), 1, 1), _
                   DateSerial(Year(DtIn) + 1, 1, 1)) = 366)
    
End Function


MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
Michael Red's suggestion is correct and simpler. A good way of doing it. The postings above had an error in them but the following Function does also work. I did it this way to show you a way of getting the answer that follows the calendar rules as they are stated above:

Public Function basLeapYear(vYourDate As Date) As Boolean
basLeapYear = Nz(Switch(DatePart("yyyy", vYourDate) Mod 1000 = 0, _
IIf(DatePart("yyyy", vYourDate) Mod 400 = 0, True, False), _
DatePart("yyyy", vYourDate) Mod 400 = 0, True, _
DatePart("yyyy", vYourDate) Mod 4 = 0, True), False)
End Function

Bob Scriver
 
Try this...

Public Function IsLeapYear(year as integer) as boolean
IsLeapYear = DateSerial(year,2,29) <> DateSerial(year,3,1)
End Function

Note: Are February 29 and March 1 different dates?
 
but it is a POOR practice to use keywords of the language as variables

MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
I agree, but the intent of the example was to show the simplest solution.
 
hmmmmmmmmmmmmmmmmmmmmmmmm ... ,

aparently our concepts of simple are at variance ...


MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
Hey Red
&quot;aparently&quot; is spelled &quot;apparently&quot;.

Where is the keyword that Creeper is using as a variable? Are you referring to the (year) he uses as an integer? If so, its not keyword, it’s a function when used like . . .

MsgBox Year(Now)

Point being, anyone can post messages pointing out trivial inconsistencies in other people posts. The most helpful posts are those that help people and answer questions.

Why pick on Creepers good example of how determine if a year is a leap year. He’s already acknowledged the fact the he shouldn’t have used the variable name (year). Excluding the variable name, Creeper’s post is good, simple, short, to the point and answer Skate's original question.

 
Creepers
Great idea to determine the LeapYear. Mine was a bit more complicated and works but your technique is certainly concise, accurate and easy to understand.

Great job. Have a star. Bob Scriver
 
hmmmmmmmmmmmmmmmmm

' ... picking on ... ' is not exactly the way I view it. it is just a comment re 'coding practice' and / terminology used. I give and get such often enough to suggest that they are a normal and useful part of the process. I consider the function more difficult to use and understand with the inclusion of the function, as it requires me to consider the use of &quot;year&quot; in a context which is different than I normally would.


MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top