If statement for range of dates using Arcade

809
4
Jump to solution
02-10-2021 05:44 PM
Labels (2)
DanielHoffman73
New Contributor II

Hello,

I'm wanting to use an IF statement symbolize and add another field for Fiscal Year.  What I'm trying to do is  code that new 'Fiscal Year' field based on a range of dates from an existing date field.  Below is what I have, though it is returning all 'N/A' values, so the logical '&&' operator doesn't appear to be selecting the dates properly.  I will say too that I'm a novice at programming, so there may be a stupidly easy syntax solution for this, and I will be humbly grateful if there is one!  

if ($feature.Projected_Date_to_Open >= 7/1/2019 && $feature.Projected_Date_to_Open <= 6/30/2020){
return 'FY 19/20';
} else if ($feature.Projected_Date_to_Open >= 7/1/2020 && $feature.Projected_Date_to_Open <= 6/30/2021) {
return 'FY 20/21';
} else if ($feature.Projected_Date_to_Open >= 7/1/2021 && $feature.Projected_Date_to_Open <= 6/30/2022) {
return 'FY 21/22';
} else {
return 'N/A';
}

Tags (4)
0 Kudos
2 Solutions

Accepted Solutions
KenBuja
MVP Honored Contributor

When you want to compare dates, you have to provide a Date object. Your code would look like this

if ($feature.Projected_Date_to_Open >= Date(2019,6,1) && $feature.Projected_Date_to_Open <= Date(2020,5,30){ //months are 0-based, where 0 is January and 11 is December

View solution in original post

XanderBakker
Esri Esteemed Contributor

Hi @DanielHoffman73 ,

In addition to what @KenBuja mentioned, it is advised that you use fields that contain underscores in a different way. See the corrected code below:

if ($feature["Projected_Date_to_Open"] >= Date(2019, 6, 1) && $feature["Projected_Date_to_Open"] <= Date(2020, 5, 30)) {
    return 'FY 19/20';
} else if ($feature["Projected_Date_to_Open"] >= Date(2020, 6, 1) && $feature["Projected_Date_to_Open"] <= Date(2021, 5, 30)) {
    return 'FY 20/21';
} else if ($feature["Projected_Date_to_Open"] >= Date(2021, 6, 1) && $feature["Projected_Date_to_Open"] <= Date(2022, 5, 30)) {
    return 'FY 21/22';
} else {
    return 'N/A';
}

 

View solution in original post

4 Replies
KenBuja
MVP Honored Contributor

When you want to compare dates, you have to provide a Date object. Your code would look like this

if ($feature.Projected_Date_to_Open >= Date(2019,6,1) && $feature.Projected_Date_to_Open <= Date(2020,5,30){ //months are 0-based, where 0 is January and 11 is December
XanderBakker
Esri Esteemed Contributor

Hi @DanielHoffman73 ,

In addition to what @KenBuja mentioned, it is advised that you use fields that contain underscores in a different way. See the corrected code below:

if ($feature["Projected_Date_to_Open"] >= Date(2019, 6, 1) && $feature["Projected_Date_to_Open"] <= Date(2020, 5, 30)) {
    return 'FY 19/20';
} else if ($feature["Projected_Date_to_Open"] >= Date(2020, 6, 1) && $feature["Projected_Date_to_Open"] <= Date(2021, 5, 30)) {
    return 'FY 20/21';
} else if ($feature["Projected_Date_to_Open"] >= Date(2021, 6, 1) && $feature["Projected_Date_to_Open"] <= Date(2022, 5, 30)) {
    return 'FY 21/22';
} else {
    return 'N/A';
}

 

DanielHoffman73
New Contributor II

Excellent.  Thanks Ken and Xander for your quick responses!  Totally works now.  Good to know about the date formatting and syntax suggestions as well.

0 Kudos
KenBuja
MVP Honored Contributor

Glad to help. Don't forget to click the "Accept as Solution" button.

0 Kudos