leap year detection

stbi

Member
Licensed User
Leap year detection is quite simple ... every year divisible by 4 unless divisible by 100 except years divisible by 400. But Basic4PPC has a "built in" :) leap year detection:

B4X:
Sub Globals
End Sub

Sub App_Start
  Form1.Show
  msgbox("2005: " & leapyear(2005))
  msgbox("2004: " & leapyear(2004))   
End Sub

sub leapyear(year)
  ' please keep correct Dateformat() in mind ...
  if DateParse("02/29/" & year) = 0 then return False else return True
end sub
DateParse() returns 0 for non-existent leap days ;) Thanks, Erel!
 

RandomCoder

Well-Known Member
Licensed User
Cool tip :cool:

Regards,
RandomCoder
 

stbi

Member
Licensed User
Thanks RandomCoder,
indeed I wrote this tip with a big smile in my face ... ;)

And here is a more "classic" leap year detection. Works with year represented by number or by datetick:

B4X:
sub DateIsLeapYear(year)
   ' transform DateTick to year if necessary
   if year > 10000 then year = DateYear(year)
   ' calculate leap year
   if (year mod 4 = 0) and (year mod 100 <> 0) or (year mod 400) = 0 then return true else return false
end sub
 

specci48

Well-Known Member
Licensed User
Just as an addition ...

With this code you don't have to worry about the actual DateFormat :):
B4X:
Sub leapyear(year)
    If DateDayOfYear(DateAdd(0, year - 1, 11, 30) = 366 Then
        Return True
    Else
        Return False
    End If
End Sub

specci48
 
Top