I had a need for a function that would return the next birth date of a person without showing the actual birthday of that person to the end user. In the beginning I thought this was going to be a cinch to do. It wasn’t until I sat down to write the function that I realized it was not going to be quite as easy as I thought.
The trick is that when you are trying to figure out someone’s next birthday you have to figure out weather they have already had a birthday in the current year or not. So to start off I had to take a birthday like 1969, 9, 12 and bring it to the current year making it 2007, 9, 12 so that I could do a comparison.
Private Function NextBirthDay(ByVal Bday As Date) As Date Dim myDay As New Date(Date.Today.Year, Bday.Month, Bday.Day) End Function
Next I had to figure out whether my new date had already passed the current date.
If myDay <= Date.Today Then myDay = myDay.AddYears(1) End If
If it had already passed I needed to add a year onto the new date so that it would return next years birthday date and not the current years birthday date. Obviously if it hadn’t already passed it is ok to return this years birthday date. The entire function looks like this
Private Function NextBirthDay(ByVal Bday As Date) As Date Dim myDay As New Date(Date.Today.Year, Bday.Month, Bday.Day) If myDay <= Date.Today Then myDay = myDay.AddYears(1) End If Return myDay End Function
And can be called using NextBirthDay(BirthDate).ToShortDateString