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;
}

Try this:

int bMon      = 3; // for March
int bDayOfMon = 26 // for March 26th

DateTime nextBirthDay = 
       (new DateTime(DateTime.Today.Year, bMon, bDayOfMon - 1 ))
        .AddDays(1).AddYears((DateTime.Today.Month > bMon || 
          (DateTime.Today.Month == bMon && 
           DateTime.Today.Day > bDayOfMon ))? 1: 0);

if your birthdate is Feb 29th this will give you the next Feb 29th, or Mar 1, depending on whether next year is leap year or not...


Does it have to be .NET code ? The easiest way to implement this in SQL will be to use an Auxiliary calendar table. There are plenty of references in google about it for example here.


Edit: Changed my example so it handles birthdays on leapday.

Function NextBirthDay(ByVal BirthDate As Date) As Date

    If Not Date.IsLeapYear(Now.Year) And BirthDate.Month = 2 And BirthDate.Day = 29 Then BirthDate.AddDays(1)

    Dim TestDate As Date = New Date(Now.Year, BirthDate.Month, BirthDate.Day)

    If DateDiff(DateInterval.Day, TestDate, Now) > 0 Then

        TestDate.AddYears(1)
        REM now check if NEXT year are leapyear, if so and birthday was a leapday, change back to leapday
        If Date.IsLeapYear(TestDate.Year) AndAlso BirthDate.Month = 2 AndAlso BirthDate.Day = 29 Then
            Return New Date(TestDate.Year, 2, 29)
        Else
            Return TestDate
        End If

    Else

        Return TestDate

    End If

End Function

Should work as expected now.


Need Your Help

Retrieving T from Task<T>

c# asynchronous parse-platform asp.net-mvc-5 async-await

I'm working on ASP.NET MVC5 app based around Parse.com framework.

How to reverse the String?

java string algorithm

how to reverse the String without using any API like reverse() , length() , toCharArray() , charAt()