Home > Flex, FlexDateUtils, code, library > Flex Date Utils – Date and Time Format (Part II)

Flex Date Utils – Date and Time Format (Part II)

December 15th, 2008 Gareth Leave a comment Go to comments

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.

  1. Chris
    September 29th, 2009 at 17:04 | #1

    Oh my god.

    I don’t know how my repeated googling for just these capabilities missed your library… but you, sir, are my freakin’ hero. I’m just learning Flex for work, and every time I have to look at a timestamp from a database I want to scream at Adobe for releasing a supposedly-complete class library without this elementary and essential tool set.

    Thank you!

  2. Gareth
    September 29th, 2009 at 21:28 | #2

    Thanks, Chris! Glad it could help you out. I know it frustrated the heck out of me at first when I couldn’t find basic date/time functions.

  3. raff
    January 20th, 2010 at 12:25 | #3

    great job :) thanks.
    I miss few functionalities, e.g. return the date of first day of selected week , but I guess I will write sth on my own on the base of your great code :)

  4. Gareth
    January 20th, 2010 at 12:45 | #4

    If there’s a function missing that you feel might be a nice addition, feel free to suggest it over at http://code.google.com/p/flexdateutils/ or http://flexdateutils.riaforge.org/ (or even here) and I’ll try to get it added. If you’ve already written something, go ahead and add that code to your suggestion, and I’ll probably be able to add it in sooner :)

    Thanks for checking out the library

  1. No trackbacks yet.