| 包 | mx.formatters |
| 类 | public class DateFormatter |
| 继承 | DateFormatter Formatter Object |
如果出现错误,则会返回一个空 String,以及一个说明已将该错误保存到 error 属性的 String。error 属性可以是下列值之一:
"Invalid value" 表示传递给 format() 方法的值既不是 Date 对象,也不是可识别的日期 String 表示形式。(允许参数为空。)
"Invalid format" 表示 formatString 属性设置为空 (""),或者 formatString 属性中的模式字母少于一个。
parseDateString() 方法使用 mx.formatters.DateBase 类定义本地化字符串信息,利用此信息可以将设置为 String 格式的日期转换为 Date 对象。
隐藏 MXML 语法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"
/>
另请参见
| 属性 | 定义方 | ||
|---|---|---|---|
![]() | constructor : Object
对类对象或给定对象实例的构造函数的引用。 | Object | |
![]() | defaultInvalidFormatError : String [静态] 为 formatter 指定的格式字符串无效时出现的错误消息。 | Formatter | |
![]() | defaultInvalidValueError : String [静态] 为 formatter 指定的值无效时出现的错误消息。 | Formatter | |
![]() | error : String 发生错误时由 formatter 保存的说明。 | Formatter | |
| formatString : String 掩码模式。 | DateFormatter | ||
![]() | prototype : Object [静态]
对类或函数对象的原型对象的引用。 | Object | |
| 方法 | 定义方 | ||
|---|---|---|---|
构造函数。 | DateFormatter | ||
根据用于设置日期格式的 String 或 Date 对象生成日期格式化的 String。 | DateFormatter | ||
![]() |
指示对象是否已经定义了指定的属性。 | Object | |
![]() |
指示 Object 类的实例是否在指定为参数的对象的原型链中。 | Object | |
![]() |
指示指定的属性是否存在、是否可枚举。 | Object | |
![]() |
设置循环操作动态属性的可用性。 | Object | |
![]() |
返回指定对象的字符串表示形式。 | Object | |
![]() |
返回指定对象的原始值。 | Object | |
| 方法 | 定义方 | ||
|---|---|---|---|
[静态] 将已设置为 String 格式的日期转换为 Date 对象。 | DateFormatter | ||
![]() | 构建 Formatter 时将调用此方法,并且每当 ResourceManager 调度“change”事件来指示本地化资源已经过某种更改时,都会再次调用此方法。 | Formatter | |
| formatString | 属性 |
formatString:String [读写] 掩码模式。
使用特定的大写字母组成模式 String,如 YYYY/MM。
DateFormatter 模式 String 既可以包含模式字母,也可以包含其它文本。要形成有效的模式 String,只需要一个模式字母。
下表描述了有效的模式字母:
| 模式字母 | 说明 |
|---|---|
| Y |
年份. 如果有两个模式字母,则年份将截断为两位数字;否则,年份将以四位数字形式出现。年份可以用零补齐,如下面一组示例中第三个示例所示:
|
| M |
一年中的月份。其格式取决于下列条件:
|
| D |
一月中的日期。尽管对于日期而言使用单个字母模式的字符串就有效,但通常使用两个字母模式的字符串。
示例:
|
| E |
星期中的天。其格式取决于下列条件:
|
| A | am/pm 标记。 |
| J | 一天中的小时数 (0-23)。 |
| H | 一天中的小时数 (1-24)。 |
| K | am/pm 中的小时数 (0-11)。 |
| L | am/pm 中的小时数 (1-12)。 |
| N |
小时中的分钟数。
示例:
|
| S |
分钟中的秒数。
示例:
|
| 其它文本。 |
可以在模式字符串中添加其它文本,以进一步设置字符串格式。可以使用标点、数字和所有小写字母。应该避免使用大写字母,因为大写字母可能被解读为模式字母。
示例:
|
默认值为 "MM/DD/YYYY".
public function get formatString():String public function set formatString(value:String):void| DateFormatter | () | 构造函数 |
public function DateFormatter()构造函数。
| format | () | 方法 |
override public function format(value:Object):String
根据用于设置日期格式的 String 或 Date 对象生成日期格式化的 String。formatString 属性用于确定输出 String 的格式。如果无法设置 value 的格式,则会返回一个空 String,并将对错误的描述写入 error 属性。
参数
value:Object — 要格式化的日期。此值可以是 Date 对象,也可以是已设置日期格式的 String,如“Thursday, April 22, 2004”。 |
String —
格式化的 String。如果发生错误,则为空字符串。将有关错误条件的描述写入 error 属性。
|
| parseDateString | () | 方法 |
protected static function parseDateString(str:String):Date将已设置为 String 格式的日期转换为 Date 对象。月份名称和日期名称必须符合 mx.formatters.DateBase 中的名称。
参数
str:String — 已设置为 String 格式的日期。 |
Date — Date 对象. |
另请参见
<?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>