Query FeatureLayer by date

5821
6
05-05-2014 04:40 PM
JimWharton
New Contributor III
Can someone give me an example of how I would query a featurelayer by date?

I have a field in my featureservice named 'time'. The field type is: type: esriFieldTypeDate

Is this documented anywhere what that field type expects? I've sent all kinds of different date formats including:

mm/dd/yyyy
yyyy-mm-dd
yyyy-mm-dd HH:MM:SS
unix_time_stamp (1399336752182)

I'm sure someone out there has queried features before or after a particular date... I'd just like to see an example as I can't find any API docs that show how this should be done.
0 Kudos
6 Replies
BenFousek
Occasional Contributor III
where = "dateField > date'YYYY/MM/DD'"


AGS 10+
0 Kudos
JohnGrayson
Esri Regular Contributor
From the ArcGIS Server REST API: feature

Note that date values are encoded as numbers. The number represents the number of milliseconds since epoch (January 1, 1970) in UTC.

Example:
query.where = "(time > " + fromDate.valueOf() + ") AND (time < " + toDate.valueOf() + ")";



JS Date Object: link
0 Kudos
ChipHankley1
New Contributor III
I had some challenges with this recently.  For me, a good starting point was to use the layer properties dialog in ArcMap to build the definition query that I wanted.  Once I had the format that ArcMap expected (for the particular layer I was working with), I could begin playing around with rendering the definition query from the JS side.

In my case, I was working against an Oracle, non-sde (native spatial type) feature class, and the definition query expected an Oracle specific date function: TO_DATE('yyyy-mm-dd hh:mm:ss','YYYY-MM-DD HH24:MI:SS').  I was unable to get the Oracle date function to work correctly, so I ended up casting the Oracle timestamp to a UNIX timestamp (milliseconds since 1/1/1970) and querying against that directly - this was easier b/c

  • I didn't have to deal with any weird date functions

  • it just becomes a number comparison condition


Word of warning about JavaScript and dates.  Make sure you carefully look at the timestamp as I believe, at some level, JavaScript always stores timezone information into the date.  I ended up having to perform a UTC offset on some of the data I was processing to make the timestamps that I was getting from Oracle match up with dates I was rendering in JS code.  Probably a mistake somewhere on my part, but something to be aware of.
0 Kudos
JimWharton
New Contributor III
I guess I must be using the wrong type of date field or something. That doesn't work for me.

Here's my service definition: https://services1.arcgis.com/BqQ60RORKMjmx3jf/ArcGIS/rest/services/Photos_Template/FeatureServer/0 (I don't think  token is required)

-Jim
0 Kudos
JohnGrayson
Esri Regular Contributor
Jim,

are you sure that's the correct url?  The service seems to be empty.  When using 'where: 1=1' and 'returnCountOnly: true' parameters:

Request:
https://services1.arcgis.com/BqQ60RORKMjmx3jf/ArcGIS/rest/services/Photos_Template/FeatureServer/0/q...

Result: { "count": 0}
0 Kudos
JimWharton
New Contributor III
The service has been regenerated, nothing exists there yet. The ESRI Date Type is the only date/time type that I can see.

Now, from all the responses here, a normal unix timestamp is what ArcGIS Online services expect as a param.

(The number of seconds from Jan 1, 1970) Is this correct?
0 Kudos