Extending DateFormatter to add 'st', 'nd', 'rd', 'th' Support.

I recently found myself with the task of configuring a date format for a new client.  The format that was specced looked something like:

Wednesday 14th October, 13:33

It was at this point I realised there is no support for the 'th' (or 'st', 'nd', 'rd') part of a date in the Flex DateFormatter.

Rather than mess around with the string after it had been formatted, I had a poke around the DateFormatter's source code and decided to write an extended date formatter which added support for date suffixes. To achieve this I needed to extend three parts of the original DateFormatter.

1. The format method of DateFormatter. This doesn't need to change much, most of what it does is validate and process the date object or string that was passed to it, before creating a StringFormatter to do the rest of the work. Unfortunately it won't let you access the processed value before it sends it on to the StringFormatter, and what gets returned from that isn't always re-usable, so I've duplicated all the processing code in my overridden format method. I've then altered the parameters that get passed to the new StringFormatter with the following 2 points.

2. The private static constant VALID_PATTERN_CHARS that gets passed to the previously mentioned StringFormatter needs changing to add a new pattern character. This can't be overridden, so I define a new static constant that includes all the normal ones, plus 'T' to represent a date suffix.

3. The internal static method extractTokenDate of the DateBase class that gets passed to the previously mentioned StringFormatter needs to be able to handle our new 'T' character. To acheive this I created my own static method that first calls extractTokenDate in DateBase, and then handles any empty result that has a token of 'T' before returning that result. Using the new DateFormatter with a 'T' in the format string will default to adding the correct 'st', 'nd', 'rd', 'th' to a date. I added a little extra customisation into this with the static var CUSTOM_NUMBER_SUFFIXES.

This can be set to a 31 element array of any custom suffix that will be appended to the date matching it's index in the array plus one.