Select to view content in your preferred language

Type of a DATE field changes to number after query

273
3
06-23-2023 12:55 AM
LefterisKoumis
Regular Contributor II

I have feature layer with a date field. 

When I check the fields before the query the field "last_edit" was "esriFieldTypeDate" - line 46

LefterisKoumis_0-1687506716536.png

After the query I checked the field and it was a number. - line 57,

LefterisKoumis_1-1687506798065.png

How do I keep the same field type?

<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
  <title>ArcGIS JavaScript Tutorials: Add your own custom button</title>
  <style>
    html, body, #viewDiv {
      padding: 0;
      margin: 0;
      height: 100%;
      width: 100%;
    }
  </style>
  
    <link rel="stylesheet" href="https://js.arcgis.com/4.16/esri/themes/light/main.css">
  <script src="https://js.arcgis.com/4.16/"></script>
  
  <script>  
    require([
      "esri/Map",
      "esri/views/MapView",
      "esri/layers/FeatureLayer"
    ], function(Map, MapView, FeatureLayer) {
      
       const layer = new FeatureLayer({
      url: "https://gis.cnra.ca.gov/arcgis/rest/services/Boundaries/CCED_AccessType/MapServer/0"
    });

      var map = new Map({
        basemap: "topo-vector",
        layers: [layer]
      });

      var view = new MapView({
        container: "viewDiv",
        map: map,
        center: [-120.679, 37.910],
        zoom: 7
      });        
      
       const btn =document.getElementById("test")      
          view.ui.add(btn, "top-right")      
         btn.addEventListener("click", querylayer);
      
      function querylayer(evt) {
       console.log(layer.fields) 

        let query = new Query();
        query.where = "1=1";
        query.outFields = ["*"];

        layer.queryFeatures(query).then(function (results) {
        console.log(results.features);
        results.features.forEach(element => {
        console.log(element.attributes)
        Object.keys(element.attributes).forEach(key => {
        console.log(key + "  " + typeof(element.attributes[key]));       
      });
    })
    // exportToShapefile(results.features);
    return;
  });

      }
    });
  </script>
</head>
<body>
  <div id="viewDiv"></div> 
   <button id="test">Test</button>
</body>
</html>

 

0 Kudos
3 Replies
Justin_Greco
Occasional Contributor III

This is the correct behavior.  Dates are returned as a number in unix time (milliseconds since 1/1/1970).  If you need to convert to a date, just do this:

 

new Date(element.getAttribute('last_edit'))

 

Also I tried putting that in Codepen and it was throwing an error that Query was not defined, since you are not requiring it.  One thing you can do instead of requiring Query is to create a query from the layer:

const query = layer.createQuery();
//instead of
let query = new Query();

If you were to query using the REST API, you'll see you do get a number returned for the date field:

https://gis.cnra.ca.gov/arcgis/rest/services/Boundaries/CCED_AccessType/MapServer/0/query?where=1=1&...

JustinGreco1_0-1687530777106.png

 

0 Kudos
LefterisKoumis
Regular Contributor II

Thank you for the reply.  I provided the above example just to demo my issue. Yes, I can convert it back to the date field as you suggested. However my actual case is an app where the user selects  a layer from a dropdown. Then, I have to access the selected layer's REST service. In the REST service some of the fields are classified as DATE and some are others are numbers. So, if I loop through the fields of the selected layer, all the fields are returned as numbers and I wouldn't know which fields are actually dates and which are numbers. Is there a way to distinguish the fields which are dates and which are not? Some of the layers have a lot of fields with a variety of field types.

0 Kudos
Justin_Greco
Occasional Contributor III

You'd want to determine the field type from the fields property of the layer (line 46).  Your line 57 is just returning the object type of that attribute for the feature, which would be number since the value is in milliseconds.

0 Kudos