Click here to Skip to main content
15,900,325 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Think i start loosing the plot here....This sort of half way work, but i think i might have been looking too much at it and now can't see the forrest for all the trees....Any idea how to improve this is welcommen
VB
Private ReadOnly CurrentDay As Date = Today
        Private Property Age As Integer = Nothing
        Private Property myBirthDay As Date = Nothing
        Private Property DaysToNextBirthDay As Long = 0
        Private Property NewBirthDate As Date = Nothing

Friend Sub CalculateNextBirthdayAndAge(ByVal ThisBirthDate As Date)
Try
myBirthDay = ThisBirthDate	'keep a copy a original birthdate

If ThisBirthDate.Month > CurrentDay.Month AndAlso ThisBirthDate.Month <= 12 Then
'Birthday is after today, but this year......Working...
NewBirthDate = CDate(FormatDateTime(CDate(String.Format("{0}-{1}-{2}", CurrentDay.Year, ThisBirthDate.Month, ThisBirthDate.Day)), DateFormat.ShortDate))
DaysToNextBirthDay = (-(DateDiff("d", NewBirthDate, CurrentDay)))

ElseIf ThisBirthDate.Month < CurrentDay.Month AndAlso ThisBirthDate.Month >= 1 Then
'Birthday was ealier this year, so increase with one year for next birthday
NewBirthDate = CDate(FormatDateTime(CDate(String.Format("{0}-{1}-{2}", CurrentDay.Year + 1, ThisBirthDate.Month, ThisBirthDate.Day)), DateFormat.ShortDate))
DaysToNextBirthDay = (DateDiff("d", CurrentDay, NewBirthDate))

ElseIf ThisBirthDate.Month = CurrentDay.Month Then
'Birthday is same month as current month.

If ThisBirthDate.Day > CurrentDay.Day Then
'Same month, but later this month
NewBirthDate = CDate(FormatDateTime(CDate(String.Format("{0}-{1}-{2}", CurrentDay.Year, ThisBirthDate.Month, ThisBirthDate.Day)), DateFormat.ShortDate))
DaysToNextBirthDay = (-(DateDiff("d", NewBirthDate, CurrentDay)))

ElseIf ThisBirthDate.Day < CurrentDay.Day Then
'same month but ealier this month
NewBirthDate = CDate(FormatDateTime(CDate(String.Format("{0}-{1}-{2}", CurrentDay.Year + 1, ThisBirthDate.Month, ThisBirthDate.Day)), DateFormat.ShortDate))
DaysToNextBirthDay = (DateDiff("d", CurrentDay, NewBirthDate))

Else
'Birthday today
MessageBox.Show("Happy Birthday", "Happy Birthday", MessageBoxButtons.OK)

End If
End If

'Age = CInt(CInt(DateDiff(DateInterval.Month, myBirthDay, Today) / 12) & " år.")
lblAge.Text = CInt(CInt(DateDiff(DateInterval.Month, myBirthDay, Today) / 12) & " år.").ToString							'Age.ToString & " år."
lblDaysToBirthDays.Text = DaysToNextBirthDay.ToString & " dage til næste fødseksdag..."

myBirthDay = Nothing
DaysToNextBirthDay = Nothing
NewBirthDate = Nothing
Age = Nothing

Catch ex As Exception
MessageBox.Show(ex.Message.ToString)
End Try
End Sub</pre>
Posted
Updated 13-Dec-17 23:20pm
v2

You can actually do math directly with dates, to produce TimeSpans:
Private Shared Function DaysToNextBirthday(ByRef DOB As DateTime) As Integer
	Dim today As DateTime = DateTime.Now
	Dim Birthday As New DateTime(today.Year, DOB.Month, DOB.Day)
	Dim diff As TimeSpan = Birthday - today
	If diff.Days < 0 Then
		' Had his birthday this year!
		diff = Birthday.AddYears(1) - today
	End If
	Return diff.Days
End Function
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Apr-11 3:22am    
My 5.
--SA
thatraja 13-Apr-11 5:02am    
Comment from OP:
uhhh...of course...Thanks....knew there much be a easier way....
You use the class DateDiff of the Time Period Library for .NET[^], to calculate the age and the next birthday (including months). Sample in C# ;)
C#
// ----------------------------------------------------------------------
public void BirthdayInfo( DateTime birthDate )
{
  DateTime now = DateTime.Now;

  // age
  Console.WriteLine( "Age: {0}", new DateDiff( now, birthDate ) );

  // next birthday
  DateTime nextBirthday = new DateTime( now.Year, birthDate.Month, birthDate.Day );
  if ( nextBirthday < now )
  {
    nextBirthday = nextBirthday.AddYears( 1 );
  }
  Console.WriteLine( "Next Birthday: {0}", new DateDiff( nextBirthday, now ) );
} // BirthdayInfo
 
Share this answer
 
Comments
VB Overlord 15-Apr-11 6:15am    
Yeah...just not really into C#...however have been playing around with since i got all of them...C++ vb, C# all part of Visual Studio 2010 ultimate, anyway thanks
Jani Giannoudis 15-Apr-11 7:19am    
You can download the library which includes the 'Pub\Desktop.Release\Itenso.TimePeriod.dll'. After adding a reference to your VB.NET project, you can adapt the few lines of my solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900