DevPinoy.org
A Filipino Developers Community
   
Calculate Age...

I got really bored today that i decided to create a demo application. There's nothing cheesy about this one since it is a simple implementation of the DateDiff function in calculating the difference of two Date values in years. I was thinking of writing something more interesting than this one but I didn't have the time to create a real braintwisting app. Anyway, enough with the crappy introduction. Here's the methods written in both C# and VB.NET:

[C# Sample]

public long CalculateAge(System.DateTime startDate)
        {
            return CalCulateAge(startDate, DateTime.Today);
        }

        public long CalculateAge(System.DateTime startDate, System.DateTime endDate)
        {
            long age = 0;
            try
            {
                if (Month(startDate) == Month(endDate))
                {
                    age = DateDiff(DateInterval.Year, startDate, endDate);
                }
                else if (Month(startDate) == Month(endDate))
                {
                    if (Day(startDate) == Day(endDate))
                    {
                        age = DateDiff(DateInterval.Year, startDate, endDate);
                    }
                    else if (Day(startDate) == Day(endDate))
                    {
                        age = DateDiff(DateInterval.Year, startDate, endDate);
                    }
                    else if (Day(startDate) == Day(endDate))
                    {
                        age = DateDiff(DateInterval.Year, startDate, endDate) - 1;
                    }
                }
                else if (Month(startDate) == Month(endDate))
                {
                    age = DateDiff(DateInterval.Year, startDate, endDate) - 1;
                }
                else
                {
                    age = 0;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return age;
        }

[VB.NET Sample]

Public Function CalculateAge(ByVal startDate As System.DateTime) As Long
Return CalculateAge(startDate, DateTime.Today)
End Function

Public Function CalculateAge(ByVal startDate As System.DateTime, ByVal endDate As System.DateTime) As Long
Dim age As Long = 0
Try
Select Case Month(startDate)
Case Is < Month(endDate)
age = DateDiff(DateInterval.Year, startDate, endDate)
Case Is = Month(endDate)
Select Case Day(startDate)
Case Is < Day(endDate)
age = DateDiff(DateInterval.Year, startDate, endDate)
Case Is = Day(endDate)
age = DateDiff(DateInterval.Year, startDate, endDate)
Case Is > Day(endDate)
age = DateDiff(DateInterval.Year, startDate, endDate) - 1
End Select
Case Is > Month(endDate)
age = DateDiff(DateInterval.Year, startDate, endDate) - 1
Case Else
age = 0
End Select
Catch ex As System.Exception
Throw ex
End Try
Return age
End Function

Geeh, this is boring... :)


Posted 12-14-2005 1:23 AM by keithrull
Filed under:

Comments

CryptoKnight wrote re: Calculate Age...
on 12-19-2005 3:33 AM
I think this is a much shorter version to calculate the age given a date. ;-)

private int CalculateAge(DateTime startDate)
{
return (DateTime.Now.Year - startDate.Year) - (startDate.AddYears(DateTime.Now.Year - startDate.Year) > DateTime.Now ? 1 : 0);
}

... and one more thing, there's no DateDiff function in C#, unless you reference the Microsoft.VisualBasic namespace
c",)
shilpa wrote re: Calculate Age...
on 02-08-2007 5:57 AM

could you please explain to me the shorter version of age calculation that u have written....tq

Pallavi wrote re: Calculate Age...
on 02-13-2007 11:08 PM

Thanx CryptoKnight. U gave nice solution.

Anonymous wrote re: Calculate Age...
on 05-10-2008 7:13 PM

How hard is it to use DateTime.Subtract?

public int GetYears(DateTime startDate)

{

    DateTime timeNow = DateTime.Now;

    TimeSpan span = timeNow.Subtract(startDate);

    return span.Years;

}

Peggy wrote re: Calculate Age...
on 06-12-2008 9:26 AM

I work in healthcare and need to be able to calculate age for adults down to newborns.  I'd like it to show age in days, months OR years - depending on the patient's age.

Centreo wrote re: Calculate Age...
on 10-27-2008 9:46 AM

How to calculate the age in months.  Answer to Peggy's question.

support.microsoft.com/.../210522

Copyright DevPinoy 2005-2008