thefrozencoder

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: AgeSpan.zip (521.59 kb)

TinyMCE, ASP.NET and validation

The following describes how to use a custom validator function along side TinyMCE to validate the contents of the textarea.

Add the custom validator and setup the properties as you normally setting the ControlToValidate to the ID of the textarea and the ClientValidationFunction to the function name you see below (this can be changed of course).

The key is to call TinyMCE's triggerSave() method so the editor can synch up to the underlying textarea. Once this is done the textarea now has content.

<script language="JavaScript" type="text/javascript">
function validateTinyMCEText(source, args) {   
var txt = document.getElementById(source.controltovalidate);
tinyMCE.triggerSave();
args.Value = (txt.value.replace(/^W+/,'')).replace(/W+$/,'');
args.IsValid = (args.Value != '');}
</script>

Hopefully this will help out a few people