Date suffixes in Flex

Apr. 21, 2008 No Comments Posted under: Random

This is a handy little function that returns the date suffix in AS3. By date suffix I mean those little letters at the end of the day of a date, such as st, rd, nd or th.

Typically languages don’t by have a function like this as the date suffixs are specific to language ( although apparently PHP can return these in Klingon ).

Most numbers have the th suffix, with x1st, x2nd and x3rd the exception. Of course 11th, 12th and 13th are the exception to the exception… See the code for more info


		public static function toLableString( date : Date ):String
		{
		public static const fullMonthLabels:ArrayCollection = new ArrayCollection( new Array("January","Febuary","March","April","May","June","July","August","September","October","November","December") );

			var dateDay : Number = date.getDate();
			var suffix : String;
			if ( dateDay % 10 == 1 && dateDay != 11 )
			{
				suffix = 'st';
			}
			else if( dateDay % 10 == 2 && dateDay != 12 )
			{
				suffix = 'nd';
			}
			else if( dateDay % 10 == 3 && dateDay != 13 )
			{
				suffix = 'rd';
			}
			else
			{
				suffix = 'th';
			}
			var day 	: String = date.date.toString();
			var month 	: String = fullMonthLabels.getItemAt( date.getMonth() ).toString();
			var year 	: String = date.getFullYear().toString();

			return month +' '+ day + suffix + ", " + year;
		}

The function will return a string that looks like this: March 2nd 2008. The name of the month comes from an array of month names, which is also quite handy :)

This entry was posted on Monday, April 21st, 2008 at 4:49 pm and is filed under Random. You can leave a comment and follow any responses to this entry through the RSS 2.0 feed.

Leave a Reply