Simple solution for parsing dates in AS3/Flex

I’ve recently had to parse dates and it turned out that Flex is not very good at it. According to specs, Date.parse() only handle following formats:

MM/DD/YYYY HH:MM:SS TZD
HH:MM:SS TZD Day Mon/DD/YYYY
Mon DD YYYY HH:MM:SS TZD
Day Mon DD HH:MM:SS TZD YYYY
Day DD Mon HH:MM:SS TZD YYYY
Mon/DD/YYYY HH:MM:SS TZD
YYYY/MM/DD HH:MM:SS TZD

Which is fine as long as you live in United States. Not all of us do so I needed to look further. There’s parseDateString method on mx.formatters.DateFormatter but it too does not provide required functionality.

So here is my implementation of date parser. It accepts specified format string and tries to parse given data accordingly. You can use D, M and Y to define this format string and any other character in it will be treated as separator and matched directly. You can refer to unit test provided together with source for usage examples. Please note that set of supported features matches my requirements and you can probably find more complete and feature rich solutions out there.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: