---
title: "在AS3和MXML中格式化日期"
author: "cloud21"
date: "2010-08-04T15:57:14Z"
tags: []
categories: ["Web前端"]
slug: "as3-mxml"
draft: false
lastmod: "2010-08-04T15:57:14Z"
id: 728951
url: "https://cloud21.iteye.com/blog/728951"
---

在项目中,我们用到格式化系统当前日期,格式化指定日期等.都是利用DateFormatter来实现的


本例分别演示如何在AS3和MXML中利用DateFormatter按照指定格式,格式化日期显示

```java
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

<mx:Script>
    <![cdata[
        import mx.formatters.*;
        [bindable]
   private var time:Date=new Date();  
        private function TestDateTimeToString():void
          {
         var fr:DateFormatter=new DateFormatter();
         fr.formatString="YYYY-MM-DD JJ:NN:SS";
            currentTimeAS3.text=fr.format(time);
          }

    ]]>    
</mx:Script>  
 <mx:DateFormatter id="dateFormat" formatString="YYYY-MM-DD JJ:NN:SS"/>
 <mx:Panel width="100%" height="100%">
  <mx:Label id="currentTimeText" text="当前日期："/>
  <mx:Label id="currentTime" text="{dateFormat.format(time)}"/>
        <mx:Button label="调用AS3转换当前日期" click="TestDateTimeToString()"/> 
  <mx:Label id="currentTimeAS3" text="现在的时间还没有被转换" />         
 </mx:Panel>
     
</mx:Application>


```