Graphic Attributes Data Types

3508
8
04-16-2012 01:04 PM
TylerWaring
Occasional Contributor II
Greetings, I am trying to write a dynamic search tool that loads all of the layer's querying and reporting dependencies into an XML file. I have done pretty good so far but ran into a problem with the date field. The dates get converted into seconds since new years in like 1970 or something. I suppose I could flag any date fields in the XML file but it doing this would make for a pretty messy schema.

I am getting the attributes using the following simplified logic:

var grAttributes:Object = graphic.attributes;
for (fld in grAttributes)
{
    //I want to test to see what the field type is here so that I
    //can convert the date vaue to something readable.
    var strValue:String = grAttributes[fld].toString();
}

Any ideas how I can get this informaiton at runtime?

Thanks, Tyler
Tags (2)
0 Kudos
8 Replies
RobertScheitlin__GISP
MVP Emeritus
Tyler,

   Date fields are always returned from ArcGIS server 10 in UTC. so it is your resposibity as the developer to convert it to a date in the format you which to present. Here is a function you could use to get a date from the UTC value:

            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.formatString = dateFormat;
                    var result:String = dateFormatter.format(date);
                    if (result)
                    {
                        return result;
                    }
                    else
                    {
                        return dateFormatter.error;
                    }
                }
                else
                {
                    return date.toLocaleString();
                }
            }


Don't forget to click the Mark as answer check on this post and to click the top arrow (promote) as shown below:
0 Kudos
TylerWaring
Occasional Contributor II
Hi Robert,

     I am trying to figure out a way to detect at runtime if the field I am accessing is a date field so I can convert the string to a readable date. Since I am not hard-coding the FIELD_NAMES and am gathering these dependencies from an XML file, I have to be able to trap for fields that are of the date field type before I can convert the string to a readable date. Is there a way to detect the field type at run time? Thanks for all of your help.

Tyler Waring
RobertScheitlin__GISP
MVP Emeritus
Tyler,

   Sure from LayerDetails that most layers inplement has a fields property and from that field array you can get the field type.
0 Kudos
TylerWaring
Occasional Contributor II
Awesome. This is just the line of logic that I need. Thanks for the conversion function as well. Both of these will definitely save me some time.
0 Kudos
LanceGoens
New Contributor III

I'm facing a very similar problem but can't seem to figure it out. It sounds like you solved this issue. Can you please post some code?

My situation may differ slightly as my date info is coming from a Table included in the map service (via QueryTask/Query). I can query it and get the long integer / ticks since epoch value from the returned FeatureSet Graphic, but like you, I can't seem to check the type on the attribute/field that contains it. The fact that the API doesn't do some checking of the attribute data type prior to displaying it's "value" seems like a major miss to me as the seconds (or whatever time measurement it uses) count since Jan 1, 1970 doesn't mean anything to any human. Also, capturing it manually is not very straight-forward as you can't get your hands on the specific Field in the Graphic's attribute collection to check the type.

I tried to do that by something like this...

for each (var field:Field in graphic.attributes) - this line results in a conversion error.

{

     if (field.type == "esriFieldTypeDate")

     {

          //Convert to something humans understand...

Please help!

Kelly Hutchins‌ I've seen you post other comments on similar issues. Do you have any recommendations?

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Lance,

   If you are using a QueryTask then the result of that is a FeatureSet and the featureset will have a fields property and there you will be able to determine the field type. The reasoning behind the ArcGIS Server and/or web api teams not automatically assigning a date format is this would lock developers into only have the date in that specific format instead of being able to choose the format that they want.

TylerWaring
Occasional Contributor II


Yep. Robert's right. I ended up doing this in my code.

  

private function featureSelectionComplete(event:FeatureLayerEvent):void

{

    for each (var field:Field in event.target.layerDetails.fields)

    {

        if (field.type == Field.TYPE_DATE)

        {

             //TODO - handle date field here   

        }

    }

}

LanceGoens
New Contributor III

Thanks for the explanation and the help. I was able to resolve my issue and am seeing dates formatted correctly.

0 Kudos