Select to view content in your preferred language

Convert miliseconds to datetime

4779
6
Jump to solution
12-02-2014 04:27 AM
EvelynHernandez
Frequent Contributor

Hello everybody,

I have a view that contains a date field and when i use the service to show the data, the service automatically return the date in miliseconds.

Is there a way to change that? Cuz the view has the field correctly and i dont know why the service changes the value.

Example: fc_evento: 1417427606000 (in arcgis srv)

It has to show 1/12/14 4:53 (in view)

Thanks

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Evelyn,

  Sorry an eGridColumn is a custom column type of my eSearch widget. So you need to just remove those references and manually set some values:

            private function getDateLbl(item:Object,column:GridColumn):String
            {
                var dateMS:Number = Number(item[column.dataField]);
                var retVal:String = "";
                var rVal:String = item[column.dataField];
                var dateFormatStr:String;
                var cuseUTC:Boolean;
                if(rVal == null){
                    //do nothing
                }else{
                    if (!isNaN(dateMS)){
                        dateFormatStr = "MM/dd/yy L:NN";
                        retVal = msToDate(dateMS, dateFormatStr, true);
                    }
                }
                return retVal;
            }

<s:GridColumn id="fc_evento" dataField="fc_evento" headerText="fc_evento" labelFunction="{getDateLbl}"></s:GridColumn>

View solution in original post

0 Kudos
6 Replies
RobertScheitlin__GISP
MVP Emeritus

Evelyn,

  It is the standard for ArcGIS Server to return a date as UTC (in milliseconds). You will have to convert that number to a date using code like this:

dateMS: is the millisecond value that you are getting back from ArcGIS Server

dateFormat: is the string representation of the date format that you desire ("MM/dd/yy L:NN").

useUTC: adjusts for timezone offset.

value = msToDate(dateMS, dateFormat, useUTC);

            private function msToDate(ms:Number, dateFormat:String, useUTC:Boolean):String
            {
                var date:Date = new Date(ms);
                if (date.milliseconds == 999){ // workaround for REST bug
                    date.milliseconds++;
                }
                if (useUTC){
                    date.minutes += date.timezoneOffset;
                }
                if (dateFormat){
                    dateFormatter.dateTimePattern = dateFormat;
                    var result:String = dateFormatter.format(date);
                    if (result){
                        return result;
                    }else{
                        return dateFormatter.errorText;
                    }
                }else{
                    return date.toLocaleString();
                }
            }
0 Kudos
EvelynHernandez
Frequent Contributor

the import for dateFormatter and dateFormatter.dateTimePattern i cannot find them.

I have tried import mx.formatters.DateFormatter;

Another question, i was doing a itemrenderer based on the gridcolumn but i cannot use the function to convert the value.

BTW, im making this for a widget.

Thanks

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Evelyn,

So you would assign the datagrid columns labelFunction then.

dgCol.labelFunction = getDateLbl;

import spark.formatters.DateTimeFormatter;
private var dateFormatter:DateTimeFormatter = new DateTimeFormatter();

          private function getDateLbl(item:Object,column:GridColumn):String
            {
                var dateMS:Number = Number(item[column.dataField]);
                var retVal:String = "";
                var rVal:String = item[column.dataField];
                var dateFormatStr:String;
                var cuseUTC:Boolean;
                if(rVal == null){
                    //do nothing
                }else{
                    if (!isNaN(dateMS)){
                        //Fix the date format to use the Spark format
                        if(column is eGridColumn){
                            dateFormatStr = (column as eGridColumn).DateFormat;
                            cuseUTC = (column as eGridColumn).useUTC;
                        }
                        dateFormatStr = dateFormatStr.replace(/D/g, "d").replace(/Y/g, "y");
                        retVal = msToDate(dateMS, dateFormatStr, cuseUTC);
                    }
                }
                return retVal;
            }
0 Kudos
EvelynHernandez
Frequent Contributor

Well, i did what u said, but im still having an issue with the eGridColumn. And i cannot see that is the problem. am I doing something wrong on the widget?

The agregarItems() function get the result for the view (4 fields) (coleccion = new ArrayCollection(featureSet.attributes); and (datos.dataProvider = coleccion;). I need to convert the fc_evento field to a datetime, cuz it is milliseconds.

Grid:

<s:DataGrid id="datos" dataProvider="{coleccion}"  resizableColumns="true" sortableColumns="true" contentBackgroundAlpha="1"  width="100%" height="90%" visible="true" editable="false" bottom="1">

  <s:columns>

  <s:ArrayList>

  <s:GridColumn id="fc_evento" dataField="fc_evento" headerText="fc_evento"></s:GridColumn>

  <s:GridColumn id="gl_ip" dataField="gl_ip" headerText="gl_ip" ></s:GridColumn>

  <s:GridColumn id="gl_param_ejec" dataField="gl_param_ejec" headerText="gl_param_ejec" ></s:GridColumn>

  <s:GridColumn id="gl_mensaje" dataField="gl_mensaje" headerText="gl_mensaje" ></s:GridColumn>

  </s:ArrayList>

  </s:columns>

  </s:DataGrid>

<s:Button id="BuscarCapex" label="Buscar" click="BuscarCapex_clickHandler(event)"></s:Button>

//Functions

protected function BuscarCapex_clickHandler(event:MouseEvent):void

  {

  // TODO Auto-generated method stub

  //IdentityManager.instance.enabled = true;

  agregarItems();

  fc_evento.labelFunction = getDateLbl;

  }

  private function getDateLbl(item:Object,column:GridColumn):String

  {

  var dateMS:Number = Number(item[column.dataField]);

  var retVal:String = "";

  var rVal:String = item[column.dataField];

  var dateFormatStr:String;

  var cuseUTC:Boolean;

  if(rVal == null){

  //do nothing

  }else{

  if (!isNaN(dateMS)){

  //Fix the date format to use the Spark format

  if(column is eGridColumn){

  dateFormatStr = (column as eGridColumn).DateFormat;

  cuseUTC = (column as eGridColumn).useUTC;

  }

  dateFormatStr = dateFormatStr.replace(/D/g, "d").replace(/Y/g, "y");

  retVal = msToDate(dateMS, dateFormatStr, cuseUTC);

  }

  }

  return retVal;

  }

  private function msToDate(ms:Number, dateFormat:String, useUTC:Boolean):String

  {

  var date:Date = new Date(ms);

  if (date.milliseconds == 999){ // workaround for REST bug

  date.milliseconds++;

  }

  if (useUTC){

  date.minutes += date.timezoneOffset;

  }

  if (dateFormat){

  dateFormatter.dateTimePattern = dateFormat;

  var result:String = dateFormatter.format(date);

  if (result){

  return result;

  }else{

  return dateFormatter.errorText;

  }

  }else{

  return date.toLocaleString();

  }

  }

Idk what im doing wrong...

Thanks for ur patience

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Evelyn,

  Sorry an eGridColumn is a custom column type of my eSearch widget. So you need to just remove those references and manually set some values:

            private function getDateLbl(item:Object,column:GridColumn):String
            {
                var dateMS:Number = Number(item[column.dataField]);
                var retVal:String = "";
                var rVal:String = item[column.dataField];
                var dateFormatStr:String;
                var cuseUTC:Boolean;
                if(rVal == null){
                    //do nothing
                }else{
                    if (!isNaN(dateMS)){
                        dateFormatStr = "MM/dd/yy L:NN";
                        retVal = msToDate(dateMS, dateFormatStr, true);
                    }
                }
                return retVal;
            }

<s:GridColumn id="fc_evento" dataField="fc_evento" headerText="fc_evento" labelFunction="{getDateLbl}"></s:GridColumn>
0 Kudos
EvelynHernandez
Frequent Contributor

Works perfectly. Thanks a lot for ur help

0 Kudos