Programming and Technology blog

Introducing The AgeSpan Class

Recently I was working on a web service that required logic than needed the span of time passed in months and days based on a start and end date.  I figured someone has already written this before me so why waste my time.  EPIC WRONG!!!!!!  While I was able to find code (mostly snippets) that did various calculations most of them were either; a) not what I needed or b) fundamentally flawed in the calculations (most choked on month/day crossovers, where the as-of-date was 1 month previous and 1 day later than the sample start date).  So I decided that I would take a chapter from the TimeSpan class and create my own AgeSpan class.

This class helps answers the question how many years, months, days, total months, total days from the birth date to the as-of-date.  So for example on Mar 04 2009 this class tells me I am 40 years; 5 months and 1 day old or 485 total month or 14762 total days old.

One of the options available is to set the include as of date in the days calculation.  What this does is include the as of date inclusively as part of the date range.  Basically it adds one day to the days and total days.  So taking the example from above I would be 40 years; 5 months and 2 days old or 485 total month or 14763 total days old.  This option is set to false by default.

Included in the zip is a unit test project to validate the actual logic.  The class is written in c# and it probably can be ported to VB pretty easily or just use the assembly as is.

AgeSpan Class

Download the code: (521.59 kb)

Comments are closed