Mike Bosch's Blog on .NET

Agile enterprise architecture in .NET, SOA, WCF, WS-*, AJAX, MVC, Sharepoint and more...

How To: Calculate the age (in years) of anything using extension methods

When creating an application that requires a person's age, we typically store the date of birth and calculate their age in years from that.  I usually store my user's date of birth in a property of type DateTime.  To make it easy to calculate the user's age based on this property, I decided to take advantage of .NET's new extension methods feature.  By adding an extension method to .NET's native DateTime type, we can quickly calculate the age of any object that has a DateTime property. 

We will create two extension methods.  One will accept no parameters and return the age based on the current date.  The overload of this method will accept an future date to base the age calculation on.  This would be useful if you wanted to know what the person's age will be on some future date.  As we see below, they are very straightforward.


Once we have that, we just need to import the namespace that our extension methods are in.  All your DateTime types will now have this method available via intellisense.

To see the effect of our new extension method, let's see what Jim Morrison's current age would be followed by how old he'll be in a number of days.  Finally, let's see how long ago the US declared independence from the British.


The previous code will produce the following output: 

 

If you want to find out more about authoring your own extension methods, check out this article

Hope this helps.

 

Posted: Jan 10 2008, 09:18 PM by MikeBosch | with 6 comment(s) |
Filed under:

Comments

vikram said:

This was a good use of extension method

# January 10, 2008 11:16 PM

Alex Hoffman said:

Wouln't the extension method be better named GetAgeInYears()?

# January 12, 2008 9:01 PM

MikeBosch said:

Yes.  That may be a more appropriate method name.  

# January 14, 2008 5:24 PM

Dave said:

Isn't the use of:

int age = (onDate - birthDate).Year;

easier to determine the age?

DateTime has an overloaded operators that handles the totaling and subtraction of dates.

# February 6, 2008 7:01 PM

Resveratrol said:

The onset of age related macular degeneration (AMD) may be related to a loss of the ability to constrict the pupil of the eye as one ages. Is there a quantitative measure or test of pupillary constrictive ability- quantifying the“ strongest constrictions

# May 22, 2008 4:15 PM

Jason Holmes said:

I'm new to asp.net and I was confused by all the code above and after goofing around for a little bit I was able to get this code to work. All I'm doing is determing the persons age based on their Birth Date that I capture when the user signs up on my site. I save the Birth Date as a datetime string. Below is the code I just used.

int ageindays = (DateTime.Now - pc.BirthDate).Days;

int age = ageindays / 365;

# June 18, 2009 11:30 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)