Leap year problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hallsers
    New Member
    • Dec 2008
    • 3

    #1

    Leap year problem

    Here is the problem i am having: -

    When a user logs in, on page load it will take the dob of all users in a stored friends list and compare them against the following conditions - To display any friends with a birthday inside the next 3 days, taking into account leap years for some1 born on the 29th of feb to be born on 28th in non leap years and also to display their age with the reminder. Here is code i have picked up so far.

    Code:
    1.  vb.net
        Public Function CalculateAge(ByVal birthday As DateTime) As Integer
         Dim bd As DateTime
         Try
          ' Birthday this year
          bd = New DateTime(DateTime.Today.Year, birthday.Month, birthday.Day)
         Catch
          ' no valid date, must be a leap year Then....
          ' set birthday one day futher in time.
          birthday = birthday.AddDays(1)
          bd = New DateTime(DateTime.Today.Year, birthday.Month, birthday.Day)
         End Try
         Dim age As Integer = DateTime.Today.Year - birthday.Year
         If (DateTime.Today < bd) Then age -= 1
         
         Return age
        End Function
    There is a problem with birthdays on Feb 29th in leap year, the statement: new DateTime(DateTi me.Today.Year, birthday.Month, birthday.Day);
    throws an exception.
    If this year is NOT a leap year, you must first convert a birthday of 2/29/ to 2/28 (or 3/1 if you think that's when the person should be a year older!).

    Code:
     2. 
    Private Sub LeapYearCheck()     
    Dim bLeapYear AsBoolean      
    bLeapYear = Date.IsLeapYear(Now.Year)     
    MessageBox.Show(bLeapYear)      
    bLeapYear = Date.IsLeapYear(2004)     
    MessageBox.Show(bLeapYear) End Sub
    Code:
    3.
    Function IsLeapYear(ByVal SomeValue As Variant) As Boolean
        On Error GoTo LocalError
        Dim intYear As Integer
        'The trick here is make sure that we get an integer
        'The 3 Golden rules are:
        'True if it is divisible by 4
        'False if it is divisible by 100
        'TRUE if it is divisble by 400
        If IsDate(SomeValue) Then
            intYear = CInt(Year(SomeValue))
        Else
            'try and get an integer from the parse
            'does not matter if we get an error
            'because the error trap will catch it
            intYear = CInt(SomeValue)
        End If
        If TypeName(intYear) = "Integer" Then
            IsLeapYear = ((intYear Mod 4 = 0) And _
              (intYear Mod 100 <> 0) Or (intYear Mod 400 = 0))
        End If
    Exit Function
    LocalError:
        IsLeapYear = False
    End Function

    I know i can use some fo this code to achieve this but i am unsure of the arrangement, any ideas?


    Cheers
    Last edited by kenobewan; Dec 8 '08, 11:43 AM. Reason: Code tags
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Originally posted by hallsers
    I know i can use some fo this code to achieve this but i am unsure of the arrangement, any ideas?

    Cheers
    I take this to mean that you have copied & pasted the code. It may better to spend the time learning how to do it and then you have a good idea next time.

    There are many online resources, books or courses that can help. Try searching the site also as I'm sure this question has come up before.

    Comment

    • hallsers
      New Member
      • Dec 2008
      • 3

      #3
      No this is code that i have found myself on the internet and am unsure as to the arrangment of such code to my specific problem. I am unsure as to what code i can use in the problem and which variables can be used or modified to suit the situation. Any help would be appreciated. Thanku

      Comment

      • hallsers
        New Member
        • Dec 2008
        • 3

        #4
        any leap year examples on the site are in another language and not vb.net
        I have attempted coding this into my Active Server Page with no luck so far.

        any ideas would be welcome or a view on which way to approach this??

        Comment

        • balabaster
          Recognized Expert Contributor
          • Mar 2007
          • 798

          #5
          This leads to the interesting question of "When does someone who is born on February 29th celebrate their birthday in non-leap years?"

          Comment

          Working...