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.
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!).
I know i can use some fo this code to achieve this but i am unsure of the arrangement, any ideas?
Cheers
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
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
Comment