Select to view content in your preferred language

DateDiff Arcade Expression for Symbology

876
7
Jump to solution
12-28-2023 11:19 AM
Labels (1)
brandonmann3
New Contributor II

I created a Survey123 for road construction which includes two date type fields, one for when the construction begins and one field for when the construction ends, and I want to symbolize the features using those two date fields. Specifically I want to symbolize the features by what happened last week, "Construction Started Last Week", "Construction Ended Last Week", "Construction Was Active Last Week", and an "Other" symbology for those features whose start construction date and end construction date are outside the last week parameters.

//Here is an expression I tried to use to symbolize this, but it didn't work, every feature had "Other" symbology:

var startedlastweek = DateDiff(Now(), $feature.start_closure,'days');

var endedlastweek = DateDiff(Now(), $feature.end_closure, 'days');

 

If (startedlastweek >= 1 && startedlastweek <= 7) {

  return "Active last week"

}

else if (endedlastweek >= 1 && endedlastweek <=7) {

  return "Ended Last Week"

} else{

  return "Other"

}

 

Thank you for the help!

0 Kudos
1 Solution

Accepted Solutions
RPGIS
by
Regular Contributor

@brandonmann3 

if ( A <= 7 && now() < $feature.start_closure )

else if ( B <= 7 && A > 7 && now() < $feature.end_closure )

 

OR

 

var start = $feature.start_closure
var end = $feature.end_closure
var A = DateDiff(Now(), start  , 'days' )
var B = DateDiff(Now(), end  , 'days' )
var Condition = Null
if ( A <= 7 && start < now()){ Condition = "Active last week" }
else if ( B <= 7 && A > 7 && end < now() ){ Condition = "Ended Last Week" }
else { Condition = "Other" }
Console( Condition )
return Condition

Insert either of the characters below in parentheses if you want to combine statements

  • && is equal to 'AND'
  • || is equal to 'OR' 

 

View solution in original post

7 Replies
jcarlson
MVP Esteemed Contributor

Try throwing in Console(startedlastweek) and see what it's returning. DateDiff usually expects the later date to come first, so you might be getting negative numbers?

- Josh Carlson
Kendall County GIS
0 Kudos
RPGIS
by
Regular Contributor

Hi @brandonmann3,

Try this:

 

 

var A = DateDiff(Now(), $feature.start_closure , 'days' )
var B = DateDiff(Now(), $feature.end_closure , 'days' )
var Condition = Null
if ( A <= 7 ){ Condition = "Active last week" }
else if ( B <= 7 && A > 7 ){ Condition = "Ended Last Week" }
else { Condition = "Other" }
Console( Condition )
return Condition

 

 

 

brandonmann3
New Contributor II

Thank you for the quick response @RPGIS it works almost perfectly. The only problem with it though is if the feature's start date is today or in the future it is symbolized as "Active last week".

Features with a start date today or in the future, and an end date of today or in future it is labeled as "Ended last week".

Is the limitation of the DateDiff function that it cannot tell the difference between positive and negative difference, and I should be using something else for this symbology? 

 

The two highlighted examples show the symbology issue I described:

brandonmann3_0-1703874703977.png

 

Thank you again.

0 Kudos
RPGIS
by
Regular Contributor

Hi @brandonmann3,

You could set it to identify if the future date is greater than now which would solve your issue.

Add  now() < $feature.start_closure to the first if statement and now() < $feature.end_closure to the second if statement and that should work.

brandonmann3
New Contributor II

@RPGIS 

Sorry I'm a bit inexperienced with arcade, where exactly do I need to put that in each line?

0 Kudos
RPGIS
by
Regular Contributor

@brandonmann3 

if ( A <= 7 && now() < $feature.start_closure )

else if ( B <= 7 && A > 7 && now() < $feature.end_closure )

 

OR

 

var start = $feature.start_closure
var end = $feature.end_closure
var A = DateDiff(Now(), start  , 'days' )
var B = DateDiff(Now(), end  , 'days' )
var Condition = Null
if ( A <= 7 && start < now()){ Condition = "Active last week" }
else if ( B <= 7 && A > 7 && end < now() ){ Condition = "Ended Last Week" }
else { Condition = "Other" }
Console( Condition )
return Condition

Insert either of the characters below in parentheses if you want to combine statements

  • && is equal to 'AND'
  • || is equal to 'OR' 

 

brandonmann3
New Contributor II

Thank you so much for your help this was great. I added an additional line to include any features created before last week and end after today.

else if (start < now() && end > now() ) {Condition = "Active last week"}
 
0 Kudos