Select to view content in your preferred language

Date format in a DataGrid

1141
7
12-09-2010 06:37 AM
KathleenBrenkert
Regular Contributor
Is there an example anywhere of how to format a date field for a datagrid? I've  tried calling a labelFunction but  anytime I apply a dateformatter using code I get nothing returned.  I've tried a few variations of the code below.

public function getDateLabel(item:Object,column:DataGridColumn):String 
{
return dateFormat.format(item[column.dataField]);
} 


Tags (2)
0 Kudos
7 Replies
RobertScheitlin__GISP
MVP Emeritus
Kathleen,

   Here is my date label function:

private function getDateLbl(item:Object,column:DataGridColumn):String
   {
    var dateMS:Number = Number(item[column.dataField]);
    var retVal:String = "";
    var rVal:String = item[column.dataField];
    if(rVal == null)
    {
     //do nothing
    }else{
     if (!isNaN(dateMS))
     {
      retVal = msToDate(dateMS, _dateFormat);
     }
    }
    return retVal;
   }
0 Kudos
KathleenBrenkert
Regular Contributor
is the msToDate a function you have written? I get an error on that line.  I'm guessing it's a millisecond to date function?
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Kathleen,

   Yep, I did forget to include that 😮

   private function msToDate(ms:Number, dateFormat:String):String
   {
    var date:Date = new Date(ms);
    if (date.milliseconds == 999) // workaround for REST bug
    {
     date.milliseconds++;
    }
    
    if (dateFormat)
    {
     dateFormatter.formatString = dateFormat;
     var result:String = dateFormatter.format(date);
     if (result)
     {
      return result;
     }
     else
     {
      return dateFormatter.error;
     }
    }
    else
    {
     return date.toLocaleString();
    }
   }
0 Kudos
KathleenBrenkert
Regular Contributor
Thanks Robert it's just what I needed.  The only problem I have now is it's not catching the rest date bug and all my dates are coming up as 1 day early.  Can you tell what I've done wrong with my msToDate code?

private function msToDate(ms:Number, dateFormat:String):String
   {
    var date:Date= new Date(ms);
    if (date.milliseconds==999)
    {
     date.milliseconds++;
     
    }
    if (dateFormat)
    {
     var result:String=mydateFormat.format(date);
     if (result)
     {
      return result;
     }
     else
     {
      return dateFormat;
     }
    }
    else
    {
     return date.toLocaleString();
    }
   }

0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Kathleen,

   Are you running ArcGIS Server SP1?
0 Kudos
KathleenBrenkert
Regular Contributor
I installed the service pack this morning but I still have the date problem.
0 Kudos
KathleenBrenkert
Regular Contributor
I changed the code to read:

date.milliseconds=date.milliseconds + 86400000; \\adds a day


I also took out the if statement to force it to add a day every time.  I'm not too happy with this fix, but it's been the only thing to get my dates to actually show up correctly in this application.  Every other web app I have programmed I add a millisecond and its works fine.
0 Kudos