What’s the quickest/neatest way to calculate the next anniversary of someone’s birthday.
For example, if I knew a person was born on 31st January, 1990, and today is the 10th February 2000, their next anniversary will be 31st January, 2001.
February 29th should roll onto March 1st (e.g. if they were born on February 29th 1990, their first birthday will be March 1st, 1991).
EDIT : Wow – I thought this would be a lot more trivial. I really assumed there would be some library function I could use. Anyhoo, thanks to all of you, I’ve got what I think is a working solution, that deals with all the stupid Feb 29th issues. It’s not very pretty though 🙁
Function NextBirthDay2(ByVal dStartDate As Date, ByVal dNow As Date) As Date Dim oDate As Date Dim bFeb29thHack As Boolean = dStartDate.Month = 2 And dStartDate.Day = 29 If bFeb29thHack Then oDate = New Date(dNow.Year, 3, 1) Else oDate = New Date(dNow.Year, dStartDate.Month, dStartDate.Day) End If If (oDate <= dNow) Then oDate = oDate.AddYears(1) End If If Date.IsLeapYear(oDate.Year) And bFeb29thHack Then oDate = oDate.AddDays(-1) End If Return oDate End Function
I haven’t worked in VB.Net, but I think the C# code will make enough sense: