Packagemx.formatters
Classpublic class DateFormatter
InheritanceDateFormatter Inheritance Formatter Inheritance Object

Language Version : ActionScript 3.0
Product Version : Flex 3
Runtime Versions : Flash Player 9, AIR 1.1


Starting with Flex 4.5, Apache recommends that you use the spark.formatters.DateTimeFormatter class as an alternative to this class.

The DateFormatter class uses a format String to return a formatted date and time String from an input 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:

The parseDateString() method uses the mx.formatters.DateBase class to define the localized string information required to convert a date that is formatted as a String into a Date object.

MXML SyntaxexpandedHide 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|Q|O|Z"
   /> 
  

View the examples

See also

mx.formatters.DateBase


Public Properties
 PropertyDefined By
 InheriteddefaultInvalidFormatError : String
[static] Error message for an invalid format string specified to the formatter.
Formatter
 InheriteddefaultInvalidValueError : String
[static] Error messages for an invalid value specified to the formatter.
Formatter
 Inheritederror : String
Description saved by the formatter when an error occurs.
Formatter
  formatString : String
The mask pattern.
DateFormatter
Protected Properties
 PropertyDefined By
 InheritedresourceManager : IResourceManager
[read-only] A reference to the object which manages all of the application's localized resources.
Formatter
Public Methods
 MethodDefined By
  
DateFormatter(formatString:String = null)
Constructor.
DateFormatter
  
format(value:Object):String
[override] Generates a date-formatted String from either a date-formatted String or a Date object.
DateFormatter
  
parseDateString(str:String, format:String = null):Date
[static] Converts a date that is formatted as a String into a Date object.
DateFormatter
Protected Methods
 MethodDefined By
 Inherited
This method is called when a Formatter is constructed, and again whenever the ResourceManager dispatches a "change" Event to indicate that the localized resources have changed in some way.
Formatter
Property Detail
formatStringproperty
formatString:String

Language Version : ActionScript 3.0
Product Version : Flex 3
Runtime Versions : Flash Player 9, AIR 1.1

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
DDay 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
EDay 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.
JHour in day (0-23).
HHour in day (1-24).
KHour in am/pm (0-11).
LHour in am/pm (1-12).
NMinute in hour.

Examples:

  • N = 3
  • NN = 03
SSecond in minute.

Example:

  • SS = 30
QMillisecond in second

Example:

  • QQ = 78
  • QQQ = 078
OTimezone offset

Example:

  • O = +7
  • OO = -08
  • OOO = +4:30
  • OOOO = -08:30
ZTimezone name

Example:

  • Z = GMT
Other textYou 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 L:NN:QQQ A = Tuesday, Sept. 8, 2005 at 1:26:012 PM

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


Implementation
    public function get formatString():String
    public function set formatString(value:String):void
Constructor Detail
DateFormatter()Constructor
public function DateFormatter(formatString:String = null)

Language Version : ActionScript 3.0
Product Version : Flex 3
Runtime Versions : Flash Player 9, AIR 1.1

Constructor.

Parameters
formatString:String (default = null) — Date format pattern is set to this DateFormatter.
Method Detail
format()method
override public function format(value:Object):String

Language Version : ActionScript 3.0
Product Version : Flex 3
Runtime Versions : Flash Player 9, AIR 1.1

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 
public static function parseDateString(str:String, format:String = null):Date

Language Version : ActionScript 3.0
Product Version : Flex 3
Runtime Versions : Flash Player 9, AIR 1.1

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. The hour value in the String must be between 0 and 23, inclusive. The minutes and seconds value must be between 0 and 59, inclusive. The following example uses this method to create a Date object:

      var myDate:Date = DateFormatter.parseDateString("2009-12-02 23:45:30"); 
The optional format property is use to work out which is likly to be encountered first a month or a date of the month for date where it may not be obvious which comes first.

Parameters

str:String — Date that is formatted as a String.
 
format:String (default = null)

Returns
Date — Date object.

See also

Examples
DateFormatterExample.mxml
<?xml version="1.0" encoding="utf-8"?>
<!--

  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

-->
<!-- Simple example to demonstrate the DateFormatter. -->
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx">

    <fx: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 = "";
                }
            }
        ]]>
    </fx:Script>

    <fx:Declarations>
        <mx:DateFormatter id="dateFormatter" formatString="month: MM, day: DD, year: YYYY"/>
        <mx:DateValidator id="dateVal" source="{dob}" property="text" inputFormat="mm/dd/yyyy"/>
    </fx:Declarations>

    <s:Panel title="DateFormatter Example" width="75%" height="75%" horizontalCenter="0" verticalCenter="0">
        <mx:Form left="10" right="10" top="10" bottom="10">
            <mx:FormItem label="Enter date (mm/dd/yyyy):" width="100%">
                <s:TextInput id="dob" text=""/>
            </mx:FormItem>
            <mx:FormItem label="Formatted date: " width="100%">
                <s:TextInput id="formattedDate" text="" editable="false"/>
            </mx:FormItem>
            <mx:FormItem>
                <s:Button label="Validate and Format" click="Format();"/>
            </mx:FormItem>
        </mx:Form>
    </s:Panel>

</s:Application>