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 1

Status
Not open for further replies.

draugen

Programmer
Nov 24, 2004
48
0
0
NO
In the program I am now writing I need to determine if a year (say 2012, or 2015). Here I divide the year by 4 to see if i get a whole number. But how do you make VB6 tell the diffrence between whole numbers (503), and non-whole numers (503.75)?
Thanks ahead of time.
 
Leap year is a little more complicated than dividing by 4. But, to answer your question, I would encourage you to use the mod operator here.

? 2015 mod 4

mod returns the remainder, so 2015 mod 4 returns 3.

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
How about

Function TestLeapYear(TestYear as Date)
dim x
On error goto FoundLeapYear
x=cvdate("29/2/" & TestYear)'or 2/29 in non English countries
TestLeapYear=TestYear & " is a normal year"
on error goto 0
exit sub

FoundLeapYear:
TestLeapYear=TestYear & " is a leap year"
Resume Leaptestdone

End function
 

Or just simple:
Code:
MsgBox IsDate("02/29/[blue]2012[/blue]")
Just replace the BLUE part with whatever Year you want.

Have fun.

---- Andy
 
wikipedia has an algorithm for calculating leap years. It's not VB6, but it's also not hard to translate it.


Code:
Public Function IsLeapYear(ByVal Year As Long) As Boolean
    
    If Year Mod 400 = 0 Then
        IsLeapYear = True
    ElseIf Year Mod 100 = 0 Then
        IsLeapYear = False
    ElseIf Year Mod 4 = 0 Then
        IsLeapYear = True
    Else
        IsLeapYear = False
    End If

End Function

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
2/29 and 29/2 become locale specific. In order to get rid of this confusion, you can use this.
[tt]IsLeap = Month(DateSerial(Year, 2, 29)) = 2
[/tt]Or[tt]
IsLeap = DateSerial(Year + 1, 1, 1) - DateSerial(Year, 1, 1) = 366
[/tt]
This one-liner is based on the definition of leap year.
[tt]IsLeap = Year Mod 400 = 0 Or Year Mod 100 And Year Mod 4 = 0[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top