Select to view content in your preferred language

Query FeatureLayer by date

2138
13
04-29-2014 05:42 AM
JimWharton
Occasional Contributor
I have a featurelayer with a field with "type: esriFieldTypeDate". I have a form on a page using the standard HTML5 date input type. (it uses the browser's built in datepicker) The format that this returns is in this format: '2014-04-29'. When I try to send this in a query, I get an error: "Unable to perform query. Please check your parameters."

What type of date format should I send in a query/querytask to be able to select dates before/after a chosen date?

I've tried using a javascript new Date(). I've tried sending it as 'YYYY-MM-DD', but nothing else.
0 Kudos
13 Replies
JakeSkinner
Esri Esteemed Contributor
Where is the data coming from, i.e. SQL Server, File Geodatabase, etc?  The following link may be helpful:

http://resources.arcgis.com/en/help/main/10.2/index.html#//00s500000033000000
0 Kudos
JimWharton
Occasional Contributor
My data is just coming from a FeatureLayer.

I'm not actually directly querying a database. When using a query (https://developers.arcgis.com/javascript/jsapi/query-amd.html) I just need to return features newer than a particular date that is set by the user.
0 Kudos
JohnGrayson
Esri Regular Contributor
Jim,
  If possible, please check the values in this field at the REST service endpoint to verify that the field values are returned by the service as an integer value that represents the number of milliseconds since midnight Jan 1, 1970 in UTC.  Looking at the json response of the query is what I would suggest.

  So, if you can create a valid Date instance from the string returned by the HTML5 widget (that truly represents the date and time you want to query with) then I would suggest you use the ".valueOf()" method on the Date instance to get the value in UTC milliseconds and use it in your query.  For example:

query.push( "time > " + (new Date(startDate)).valueOf() );


More info on Date objects: JavaScript Date Object

If this does not work, then it would help if we had a public map service endpoint that your query is trying to access so we can figure out how to properly do this date query.
0 Kudos
JimWharton
Occasional Contributor
Ok, doing that (turning it into a standard Unix timestamp, gives me this as a query param: where:time > 1398729600000

That looks correct, but I'm still getting the same error.

The entire method looks like this:

        buildQuery: function() {
          var query = [];
          var name = this.username.value;
          var startDate = this.startDate.value;
          var endDate = this.endDate.value;

          if(name) {
            query.push("username LIKE '%"+name+"%'");
          }

          if(startDate) {
            query.push("time > "+new Date(startDate).valueOf());
          }

          if(endDate) {
            query.push("time < "+new Date(endDate).valueOf());
          }

          if(query.length > 1) {
            return query.join(' AND ');
          } else {
            return query.join('');
          }
        },



The feature layer definition has list of fields is as follows:

Fields:
FID (type: esriFieldTypeInteger, alias: FID, SQL Type: sqlTypeInteger, nullable: false, editable: false)
username (type: esriFieldTypeString, alias: username, SQL Type: sqlTypeNVarchar, length: 254, nullable: true, editable: true)
lat (type: esriFieldTypeDouble, alias: lat, SQL Type: sqlTypeFloat, nullable: true, editable: true)
lng (type: esriFieldTypeDouble, alias: lng, SQL Type: sqlTypeFloat, nullable: true, editable: true)
accuracy (type: esriFieldTypeDouble, alias: accuracy, SQL Type: sqlTypeFloat, nullable: true, editable: true)
heading (type: esriFieldTypeDouble, alias: heading, SQL Type: sqlTypeFloat, nullable: true, editable: true)
time (type: esriFieldTypeDate, alias: time, SQL Type: sqlTypeTimestamp2, length: 8, nullable: true, editable: true)
photo (type: esriFieldTypeString, alias: photo, SQL Type: sqlTypeNVarchar, length: 254, nullable: true, editable: true)
photothumb (type: esriFieldTypeString, alias: photothumb, SQL Type: sqlTypeNVarchar, length: 254, nullable: true, editable: true)
0 Kudos