Select to view content in your preferred language

Arcade expression for date ranges

2695
8
Jump to solution
11-13-2020 09:11 AM
KhemAryal
Regular Contributor

can some one help me correct the expression for the maintenance done in different date ranges

example:

var daterange = date(2020,1)<(2020,7)
var daterange1 = date(2020,8)<(2020,12)
var maint =DomainName($feature,"MAINTENANCE_TYPE") (it has 3 domains as regular, non-scheduled, and emergency)

if (maint=daterange) {
return "Spring-Summer";
if (maint=daterange1){
return "Fall-Winter";
} else {
return "Other"
}

 

 

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

Hi @KhemAryal  and @JoshuaBixby ,

 

See the example below:

// change the name of the field
var datemonth = Month($feature["Name of the Date Field"]); 
var maint = Proper(DomainName($feature,"MAINTENANCE_TYPE"), "firstword");

if (datemonth < 7) {
    return maint + " Spring";
} else {
    return maint + " Fall";
}

 

Note that the month value is a value from 0 to 11 and not 1 to 12. So spring (including July) should be a value < 7.

View solution in original post

Tags (3)
8 Replies
JoshuaBixby
MVP Esteemed Contributor

There are several things to comment on, but I don't have time to hit on them all now, so I will touch on the biggest.

First, you are making bad Date function calls.  You have to specify a day as well, i.e., Date(2020,1,1).  Second, the way you are trying to define a date range is not correct.  If your Date function calls were correct, what you would be storing in daterange and daterange1 is not a date range but a boolean true value because January is less than July.

KhemAryal
Regular Contributor

 

Hi, thanks for the information,

I am very new to arcade and trying to learn stuff here. In my example, I have Date field and Maintenance Field (with three domains regular, non-schedule, emergency)

I am trying to show in symbol with arcade expression

emergency maintenance done between

January to July as Emergency Spring

August to December as Emergency "fall"

regular maintenance done between

January to July as Regular Spring

August to December as Regular "fall"

non-schedule maintenance done between

January to July as non-schedule Spring

August to December as non-schedule "fall"

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

The following structure should work:

 

IIf(Month(maint_date) < 8, "spring", "fall")

 

 

 

XanderBakker
Esri Esteemed Contributor

Hi @KhemAryal  and @JoshuaBixby ,

 

See the example below:

// change the name of the field
var datemonth = Month($feature["Name of the Date Field"]); 
var maint = Proper(DomainName($feature,"MAINTENANCE_TYPE"), "firstword");

if (datemonth < 7) {
    return maint + " Spring";
} else {
    return maint + " Fall";
}

 

Note that the month value is a value from 0 to 11 and not 1 to 12. So spring (including July) should be a value < 7.

Tags (3)
JoshuaBixby
MVP Esteemed Contributor

Xander, regarding the month cut off, I was assuming the OP didn't want a 6/6 split but a 7/5 split for defining seasons given what he wrote in response to my first reply.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi @JoshuaBixby ,

 

According to the information provided by the OP, the month is used to assign January to July to Spring and August to December to Fall. Since the Month function on a date in July will be return the value 6, the expression should check for values < 7 to assign Spring and Fall for all other values.

See the sample below:

var aDate = Date(2020, 6, 22);
Console(aDate);
var datemonth = Month(aDate); 
Console(datemonth);
var maint = "Non-schedule";

if (datemonth < 7) {
    return maint + " Spring";
} else {
    return maint + " Fall";
}

 

This will write to the console:

2020-07-22T00:00:00-05:00
6

 

The date is in July and the month will return 6, so the value returned is "Non-schedule Spring"

JoshuaBixby
MVP Esteemed Contributor

Ah, right, the JavaScript-esque of Arcade.  I have always found those languages that treat month indexing starting at 0 as odd because they don't starting day indexing at 0.  Treating months of the year differently than days of the week is silly, but I guess we can blame some smarty decades ago for starting that nonsense.

XanderBakker
Esri Esteemed Contributor

Hi @JoshuaBixby ,

You're not the only one that has made mistakes with this zero index month weirdness that is part of JavaScript. I have been there as well (and more than once...).