views:

1954

answers:

3

I have this code :

var dp:Array = new Array();

for ( var i:int = 0; i < e.result.length; i++ ) { var row:Object = e.result[i];

       dp.push( row  );

}

The row object consists data for different columns of a datagrid. dp.push(row) pushes the data onto the datagrid.

Columns with index 3 and 4 have type of "DATE" with whole time stamp being displayed. Is there any method in flex which will help me extract the monnth/day/year from date or timestamp for that matter.

If yes, How do I do it in code.

Please help.

A: 

The as3corelib library has a utility class to parse timestamp strings into Date instances. Look into using the DateUtil class, specifically one of the parse methods such as parseW3CDTF.

From the resulting Date instance, you can examine the individual date properties then.

var dt:Date = DateUtil.parseW3CDTF( timestamp );
trace( dt.year );
darronschall
can I not directly extract and modify the row object ?Will this work :: if(row.columnIndex==3) { row.data=(row.data).getFullYear(); }??
+1  A: 

After re-reading your question, it sounds like you're looking to change the display of the date object in the datagrid itself? For that, you should look at using a labelFunction for the DataGridColumn that is displaying the Date instance.

<mx:DataGridColumn labelFunction="dateFormatLabelFunction" />

private function dateFormatLabelFunction( item:Object, column:DataGridColumn ):String 
{ 
    return item.date.day + "/" ; //...
}

Or, alternatively, use the DateFormatter to format the date in the label function:

<mx:DateFormatter id="dateFormatter" format="MM/DD/YYYY" />

private function dateFormatLabelFunction( item:Object, column:DataGridColumn ):String 
{ 
    return dateFormatter.format( item.date );
}

EDIT: Per comments, the combined approach code sample would look something like this:

<mx:Script>
    <![CDATA[
        private function dateFormatLabelFunction( item:Object, column:DataGridColumn ):String 
        { 
            return dateFormatter.format( item[ column.dataField ] );
        } 
    ]]>
</mx:Script>

<mx:DateFormatter id="dateFormatter" format="MM/DD/YYYY" />

<mx:DataGrid ...>
    <mx:columns>
        <mx:DataGridColumn dataField="myDateField" labelFunction="dateFormatLabelFunction" />
    </mx:columns>
</mx:DataGrid>
darronschall
Ok..I will check this out..
If I have to use the second alternative, where exactly will I have to write the DateFormatter tag ??<mx:DataGridColumn width="75" dataField="myDate" headerText=" Date" editable="false"/>This is my Datagridcolumn where I have to write the date. How do I go about for the DateFormatter tag with this and also ensure that the source is passed correctly ??
The second example uses a label function as well on the data grid column. I was just showing you how to use the DateFormatter class to format the date inside of the label function rather than doing that by hand like the first example does.The easiest way is to define the <mx:DateFormatter /> somewhere in your .mxml file. I'll edit this answer to describe the combined approach better.
darronschall
I tried the codeprovided. But, compilation gives error saying 'null'. java.lang.nullPointer exception. Is the syntax correct ?
Also, the datagrid is my top level tag as am having this as a separate component. Inside that<?xml version="1.0" encoding="utf-8"?><mx:DataGrid creationComplete="init()" itemClick="itemClickEvent(event)"><mx:DateFormatter id="dateFormatter" formatString="MM/DD/YYYY" /><mx:Metadata> [Event(name="myEvent", type="utils.myEvent")]</mx:Metadata>.....Is it the correct positioning of dateFormatter tag ?
Thanks. Actually the code is correct. Had some other error. Thanks.
A: 

thanks that worked

Flexible