6 Replies Latest reply on Aug 24, 2010 6:48 AM by JoshBeall

    Proper way to format a date object when displaying in a datagrid

    JoshBeall Level 1

      Hi All,


      I've got a datagrid that displays an ArrayCollection of objects that has a Date property.  I want to format the Date property to display as MM/DD/YYYY.


      I was able to accomplish that like this.  Is there a better way?  This seems a bit complicated and verbose.  I was hoping to be able to just apply an attribute to the DataGridColumn, something like "displayFormat" that would tell it how to parse the date into a string.


      Anyway, here's what I did.  First, inside the <fx:Declarations> block I've got <mx:DateFormatter id='dateTimeFormatter' formatString='MM/DD/YYYY' />


      Then, I've defined the following function inside an <fx:Script> block:




      public function labelFunction(item:Object, column:DataGridColumn):String{

        var result:String = "";


          case 'expiration':

          result = dateTimeFormatter.format(item.expiration);



        return result;



      Then, I've defined my data grid like this:



      <mx:DataGrid x="10" y="42" dataProvider="{currentRoster}">


          <mx:DataGridColumn headerText="Member ID" dataField="constituentId"/>

          <mx:DataGridColumn headerText="Firstname" dataField="firstname"/>

          <mx:DataGridColumn headerText="Lastname" dataField="lastname"/>

          <mx:DataGridColumn headerText="Standing" dataField="standing"/>

          <mx:DataGridColumn headerText="Expiration" dataField="expiration" labelFunction="labelFunction" /




      And that works.  But again, it seems like a bit much when all I want to do is change the way the "Expiration" column displays the date?