leap year detection

Discussion in 'Code Samples & Tips' started by stbi, Jul 1, 2007.

  1. stbi

    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:

    Code:
    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!
     
  2. RandomCoder

    RandomCoder Well-Known Member Licensed User

    Cool tip :cool:

    Regards,
    RandomCoder
     
  3. stbi

    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:

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

    specci48 Well-Known Member Licensed User

    Just as an addition ...

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

    specci48
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice