Adobe Flex 2 Language Reference - collected by Jimbob | Back to MY RESOURCES


Packagemx.formatters
Classpublic class DateFormatter
InheritanceDateFormatter Inheritance Formatter Inheritance Object

The DateFormatter class uses a format String to render date and time Strings from a String or a Date object. You can create many variations easily, including international formats.

If an error occurs, an empty String is returned and a String describing the error is saved to the error property. The error property can have one of the following values:

MXML Syntaxexpanded Hide MXML Syntax

You use the <mx:DateFormatter> tag to render date and time Strings from a Date object.

The <mx:DateFormatter> tag inherits all of the tag attributes of its superclass, and adds the following tag attributes:

  <mx:DateFormatter
    formatString="Y|M|D|A|E|H|J|K|L|N|S"
   /> 
  

View the examples.

See also

mx.formatters.DateBase


Public Properties
 PropertyDefined by
 Inheritedconstructor : Object
A reference to the class object or constructor function for a given object instance.
Object
 Inheritederror : String
Description saved by the formatter when an error occurs.
Formatter
  formatString : String
The mask pattern.
DateFormatter
 Inheritedprototype : Object
[static] A reference to the prototype object of a class or function object.
Object
Protected Properties
 PropertyDefined by
 InheriteddefaultInvalidFormatError : String = "Invalid format"
Error message for an invalid format string specified to the formatter.
Formatter
 InheriteddefaultInvalidValueError : String = "Invalid value"
Error message for an invalid value specified to the formatter.
Formatter
 InheritedpackageResources : ResourceBundle
[static] A ResourceBundle object containing all symbols from formatters.properties.
Formatter
Public Methods
 MethodDefined by
  
Constructor.
DateFormatter
  
Generates a date-formatted String from either a date-formatted String or a Date object.
DateFormatter
 Inherited
setPropertyIsEnumerable(name:String, isEnum:Boolean = true):void
Sets the availability of a dynamic property for loop operations.
Object
 Inherited
Returns the string representation of the specified object.
Object
 Inherited
Returns the primitive value of the specified object.
Object
Protected Methods
 MethodDefined by
  
[static] Converts a date that is formatted as a String into a Date object.
DateFormatter
Property detail
formatStringproperty
public var formatString:String

The mask pattern.

You compose a pattern String using specific uppercase letters, for example: YYYY/MM.

The DateFormatter pattern String can contain other text in addition to pattern letters. To form a valid pattern String, you only need one pattern letter.

The following table describes the valid pattern letters:

Pattern letterDescription
Y Year. If the number of pattern letters is two, the year is truncated to two digits; otherwise, it appears as four digits. The year can be zero-padded, as the third example shows in the following set of examples:
  • YY = 05
  • YYYY = 2005
  • YYYYY = 02005
M Month in year. The format depends on the following criteria:
  • If the number of pattern letters is one, the format is interpreted as numeric in one or two digits.
  • If the number of pattern letters is two, the format is interpreted as numeric in two digits.
  • If the number of pattern letters is three, the format is interpreted as short text.
  • If the number of pattern letters is four, the format is interpreted as full text.
Examples:
  • M = 7
  • MM= 07
  • MMM=Jul
  • MMMM= July
D Day in month. While a single-letter pattern string for day is valid, you typically use a two-letter pattern string.

Examples:

  • D=4
  • DD=04
  • DD=10
E Day in week. The format depends on the following criteria:
  • If the number of pattern letters is one, the format is interpreted as numeric in one or two digits.
  • If the number of pattern letters is two, the format is interpreted as numeric in two digits.
  • If the number of pattern letters is three, the format is interpreted as short text.
  • If the number of pattern letters is four, the format is interpreted as full text.
Examples:
  • E = 1
  • EE = 01
  • EEE = Mon
  • EEEE = Monday
A am/pm indicator.
J Hour in day (0-23).
H Hour in day (1-24).
K Hour in am/pm (0-11).
L Hour in am/pm (1-12).
N Minute in hour.

Examples:

  • N = 3
  • NN = 03
S Second in minute.

Example:

  • SS = 30
Other text You can add other text into the pattern string to further format the string. You can use punctuation, numbers, and all lowercase letters. You should avoid uppercase letters because they may be interpreted as pattern letters.

Example:

  • EEEE, MMM. D, YYYY at H:NN A = Tuesday, Sept. 8, 2005 at 1:26 PM

The default value is "MM/DD/YYYY".

Constructor detail
DateFormatter()constructor
public function DateFormatter()

Constructor.

Method detail
format()method
public override function format(value:Object):String

Generates a date-formatted String from either a date-formatted String or a Date object. The formatString property determines the format of the output String. If value cannot be formatted, return an empty String and write a description of the error to the error property.

Parameters
value:Object — Date to format. This can be a Date object, or a date-formatted String such as "Thursday, April 22, 2004".

Returns
String — Formatted String. Empty if an error occurs. A description of the error condition is written to the error property.
parseDateString()method 
protected static function parseDateString(str:String):Date

Converts a date that is formatted as a String into a Date object. Month and day names must match the names in mx.formatters.DateBase.

Parameters
str:String — Date that is formatted as a String.

Returns
Date — Date object.

See also

Examples
DateFormatterExample
<?xml version="1.0" encoding="utf-8"?>
<!-- Simple example to demonstrate the DateFormatter. -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

    <mx:Script>
        <![CDATA[

            import mx.events.ValidationResultEvent;            
            private var vResult:ValidationResultEvent;

            // Event handler to validate and format input.            
            private function Format():void
            {
                vResult = dateVal.validate();
                if (vResult.type==ValidationResultEvent.VALID) {
                    formattedDate.text=dateFormatter.format(dob.text);
                }
              
                else {
                    formattedDate.text= "";
                }
            }
        ]]>
    </mx:Script>

    <mx:DateFormatter id="dateFormatter" formatString="month: MM, day: DD, year: YYYY"/>

    <mx:DateValidator id="dateVal" source="{dob}" property="text" inputFormat="mm/dd/yyyy"/>

    <mx:Panel title="DateFormatter Example" width="95%" height="95%" 
            paddingTop="10" paddingLeft="5" paddingRight="5" paddingBottom="10">

        <mx:Form width="100%">
            <mx:FormItem label="Enter date (mm/dd/yyyy):" width="100%">
                <mx:TextInput id="dob" text=""/>
            </mx:FormItem>

            <mx:FormItem label="Formatted date: " width="100%">
                <mx:TextInput id="formattedDate" text="" editable="false"/>
            </mx:FormItem>

            <mx:FormItem>
                <mx:Button label="Validate and Format" click="Format();"/>
            </mx:FormItem>
        </mx:Form>

    </mx:Panel>
</mx:Application>





collected by Jimbob 2007.05