This post is the second part of my description of the date and time format methods contained in the DateUtils class. I will be going over the public methods this time and showing how to call them from your code.
// Date masks
public static const SHORT_DATE_MASK:String = "MM/DD/YY";
public static const MEDIUM_DATE_MASK:String = "MMM D, YYYY";
public static const LONG_DATE_MASK:String = "MMMM D, YYYY";
public static const FULL_DATE_MASK:String = "EEEE, MMMM D, YYYY";
// Time masks
public static const SHORT_TIME_MASK:String = "L:NN A";
public static const MEDIUM_TIME_MASK:String = "L:NN:SS A";
// TZD = TimeZoneDesignation = GMT + or - X hours, non-standard, requires a slight hack
public static const LONG_TIME_MASK:String = MEDIUM_TIME_MASK + " TZD";
These are the constants that can be used to denote the masks to use when calling the date and time formatters. This makes the coder less prone to typing out incorrect values when entering a date. User defined masks can still be passed as parameters, but the predefined masks should cover 99% of everyone’s needs.
/**
* Formats a time into a certain time format
*
* @param date The date to format
* @param mask How the date should be formatted
*
* @return A formatted time
*/
public static function timeFormat( date:Date, mask:String=DateUtils.SHORT_TIME_MASK ):String {
return buildDateTime( date, mask, "(A|:|J|H|K|L|N|S|TZD|\\s)+", DateUtils.SHORT_TIME_MASK );
}
This method formats the date to a specified time. The standard output uses the SHORT_TIME_MASK (L:NN A which creates in the format 12:05 am), but can be altered to whatever format the programmer wants. I pass the date to the buildDateTime method (described in a previous post) to build the time, making sure to pass what constitutes a valid mask in the regular expression pattern. The default is the SHORT_TIME_MASK again.
To use this method, import the package:
import com.flexoop.utilities.dateutils.DateUtils;
then:
private var _formatted:String = DateUtils.timeFormat( new Date() );
This would take the current time and would output it like this: 11:16 pm
/**
* Formats a date into a certain date format
*
* @param date The date to format
* @param mask How the date should be formatted
*
* @return A formatted date
*/
public static function dateFormat( date:Date, mask:String=DateUtils.SHORT_DATE_MASK ):String {
return buildDateTime( date, mask, "(Y|M|D|E|\\W)+", DateUtils.SHORT_DATE_MASK );
}
This method formats the date to a specified time. The standard output uses the SHORT_DATE_MASK (MM/DD/YY which creates in the format 12/15/08), but can be altered to whatever format the programmer wants. I pass the date to the buildDateTime method (described in a previous post) to build the date, making sure to pass what constitutes a valid mask in the regular expression pattern. The default is the SHORT_DATE_MASK again.
To use this method, import the package:
import com.flexoop.utilities.dateutils.DateUtils;
then:
private var _formatted:String = DateUtils.dateFormat( new Date() );
This would take the current date and would output it like this: 12/15/08
/**
* Formats a date into a certain date/time format
*
* @param date The date to format
* @param mask How the date should be formatted
*
* @return A formatted date
*/
public static function dateTimeFormat( date:Date, mask:String="MM/DD/YYYY L:NN:SS A" ):String {
return buildDateTime( date, mask, "(Y|M|D|E|A|J|H|K|L|N|S|TZD|\\W)+", DateUtils.SHORT_DATE_MASK + ' ' + DateUtils.SHORT_TIME_MASK );
}
And finally,
This method formats the date to a specified date and time. The standard output uses a combination of the SHORT_DATE_MASK (MM/DD/YY which creates in the format 12/15/08) and the SHORT_TIME_MASK (L:NN A which creates in the format 12:05 am), but can be altered to whatever format the programmer wants. I pass the date to the buildDateTime method (described in a previous post) to build the date and time, making sure to pass what constitutes a valid mask in the regular expression pattern. The default is the SHORT_DATE_MASK and the DateUtils.SHORT_TIME_MASK again.
To use this method, import the package:
import com.flexoop.utilities.dateutils.DateUtils;
then:
private var _formatted:String = DateUtils.dateTimeFormat( new Date() );
This would take the current date and would output it like this: 12/15/08 11:20 pm
Once again, a very useful function that did not get included in the Flex framework for whatever reason, but now can easily be include in anyone’s code.
All methods in this series are describing code in the FlexDateUtils package I put on Google code.