Select to view content in your preferred language

Featurelayer DefinitionExpression based on Date Range

4641
6
Jump to solution
08-31-2015 10:22 AM
SteveCole
Honored Contributor

I have a layer of construction closures in an app I'm developing and for each closure, there is a field for the starting date of the closure (startDate) and a field for the ending date of the closure (endDate). Ideally, I'd like to add a definitionExpression to the FeatureLayer to automatically handle the visibility of any current closures.

I know I can't just use:

var curDate = new Date();

var fLayer = new FeatureLayer("url", {
    definitionExpression:  "endDate <= Date " + curDate;
    mode: FeatureLayer.MODE_ONDEMAND
});

because that would show current projects but also older, expired projects. So how would I structure the definition expression?

Thanks!

Steve

0 Kudos
1 Solution

Accepted Solutions
RobertWinterbottom
Deactivated User

This partially depends on how the data is stored in the service. For an esriFieldTypeDate. We have used the following to some success, this was from a filter that said show everything in the last 48 hours.

ACQ_DATE > date '2015-08-28 15:19:35'

However since you want to show things in between two dates, you could use something like this:

startDate > date '2015-08-28 15:19:35' and endDate < date '2015-08-31 15:19:35';

The date strings (eg. '2015-08-28 15:19:35') were formatted using JavaScripts Date class in my case, but they are getting sent in as strings so you can construct them in whatever manner is most appropriate for your application.  Hope this helps.

View solution in original post

6 Replies
thejuskambi
Frequent Contributor

You need to define what current closure mean, today, this week or this month. and add between clause according.

0 Kudos
SteveCole
Honored Contributor

Let's say I open the app today, which is August 31st. Given that, here are some scenarios:

CLOSURE 1: startDate: August 1st, endDate: August 31st (closure should display)

CLOSURE 2: startDate: July 15th, endDate July 31st (closure should not display)

CLOSURE 3: startDate: August 25th, endDate: September 2nd (closure shoudld display)

CLOSURE 4: startDate: September 1st, endDate: September 2nd (closure should not display)

Basically, as long as today's date lies between the startDate and endDate values, the closure should display.

0 Kudos
thejuskambi
Frequent Contributor

I dont understand what you are looking for. If getting the current date is the problem then below line should help you.

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();

0 Kudos
RobertWinterbottom
Deactivated User

This partially depends on how the data is stored in the service. For an esriFieldTypeDate. We have used the following to some success, this was from a filter that said show everything in the last 48 hours.

ACQ_DATE > date '2015-08-28 15:19:35'

However since you want to show things in between two dates, you could use something like this:

startDate > date '2015-08-28 15:19:35' and endDate < date '2015-08-31 15:19:35';

The date strings (eg. '2015-08-28 15:19:35') were formatted using JavaScripts Date class in my case, but they are getting sent in as strings so you can construct them in whatever manner is most appropriate for your application.  Hope this helps.

SteveCole
Honored Contributor

Thanks, Robert. My date fields are defined as esriFieldTypeDate so your example should work with my situation.

0 Kudos
SteveCole
Honored Contributor

Just to wrap a bow on this thread, I used Robert's suggestion but had to switch the less than/greater than signs to get it to work properly:

    var today = new Date();
    var theDefExp = "startDate < date'" + today.toLocaleDateString() + "' and endDate > date '" + today.toLocaleDateString() + "'";
    constructionPtLayer.setDefinitionExpression(theDefExp);
0 Kudos