Connecting Tech Pros Worldwide Forums | Help | Site Map

Leap year problem

Newbie
 
Join Date: Dec 2008
Posts: 3
#1: Dec 8 '08
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.

Expand|Select|Wrap|Line Numbers
  1. 1.  vb.net
  2.     Public Function CalculateAge(ByVal birthday As DateTime) As Integer
  3.      Dim bd As DateTime
  4.      Try
  5.       ' Birthday this year
  6.       bd = New DateTime(DateTime.Today.Year, birthday.Month, birthday.Day)
  7.      Catch
  8.       ' no valid date, must be a leap year Then....
  9.       ' set birthday one day futher in time.
  10.       birthday = birthday.AddDays(1)
  11.       bd = New DateTime(DateTime.Today.Year, birthday.Month, birthday.Day)
  12.      End Try
  13.      Dim age As Integer = DateTime.Today.Year - birthday.Year
  14.      If (DateTime.Today < bd) Then age -= 1
  15.  
  16.      Return age
  17.     End Function
There is a problem with birthdays on Feb 29th in leap year, the statement: new DateTime(DateTime.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!).

Expand|Select|Wrap|Line Numbers
  1.  2. 
  2. Private Sub LeapYearCheck()     
  3. Dim bLeapYear AsBoolean      
  4. bLeapYear = Date.IsLeapYear(Now.Year)     
  5. MessageBox.Show(bLeapYear)      
  6. bLeapYear = Date.IsLeapYear(2004)     
  7. MessageBox.Show(bLeapYear) End Sub
Expand|Select|Wrap|Line Numbers
  1. 3.
  2. Function IsLeapYear(ByVal SomeValue As Variant) As Boolean
  3.     On Error GoTo LocalError
  4.     Dim intYear As Integer
  5.     'The trick here is make sure that we get an integer
  6.     'The 3 Golden rules are:
  7.     'True if it is divisible by 4
  8.     'False if it is divisible by 100
  9.     'TRUE if it is divisble by 400
  10.     If IsDate(SomeValue) Then
  11.         intYear = CInt(Year(SomeValue))
  12.     Else
  13.         'try and get an integer from the parse
  14.         'does not matter if we get an error
  15.         'because the error trap will catch it
  16.         intYear = CInt(SomeValue)
  17.     End If
  18.     If TypeName(intYear) = "Integer" Then
  19.         IsLeapYear = ((intYear Mod 4 = 0) And _
  20.           (intYear Mod 100 <> 0) Or (intYear Mod 400 = 0))
  21.     End If
  22. Exit Function
  23. LocalError:
  24.     IsLeapYear = False
  25. End Function

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


Cheers

kenobewan's Avatar
Moderator
 
Join Date: Dec 2006
Posts: 4,745
#2: Dec 8 '08

re: Leap year problem


Quote:

Originally Posted by hallsers View Post

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.
Newbie
 
Join Date: Dec 2008
Posts: 3
#3: Dec 8 '08

re: Leap year problem


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
Newbie
 
Join Date: Dec 2008
Posts: 3
#4: Dec 8 '08

re: Leap year problem


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??
balabaster's Avatar
Moderator
 
Join Date: Mar 2007
Location: Canada
Posts: 757
#5: Dec 9 '08

re: Leap year problem


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

Tags
leap year.


Similar Visual Basic .NET bytes