Calculate the next anniversary date after today

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

Answers


I haven't worked in VB.Net, but I think the C# code will make enough sense:

private DateTime nextDate(DateTime currentDate, DateTime anniversaryDate)
{
    DateTime nextDate;
    try{
        nextDate = new DateTime(currentDate.Year, anniversaryDate.Month, anniversaryDate.Day);
    } catch (ArgumentOutOfRangeException)
    {
        //for 29 Feb case.
        nextDate = new DateTime(currentDate.Year, anniversaryDate.Month, anniversaryDate.Day-1).AddDays(1);
    }

    if (nextDate <= currentDate)
        nextDate = nextDate.AddYears(1);
    return nextDate;
}

Need Your Help

How do I attract developers to an Open Source project?

open-source

How do I attract developers to an open source project? Obviously if the project were cool or valuable it would be easier to find people. (In fact, they would probably come to me.) But what do I do ...

Can't convert int object to str implicitly error

python

Here is a simplified snapshot of my code: