Arcade Expression - Inspection Date in Current Quarter of Year

139
4
08-01-2022 08:42 AM
New Contributor III

Hello!

I am trying to figure out how to write an Arcade expression that will determine if an inspection date occurred within the current quarter of the year (Jan-Mar, Apr-Jun, Jul-Sep, Oct-Dec).  My sense is that I need to convert the current date to quarter and the inspection date to quarter and see if they match, but I am not sure of the details for how to set that up.  Anyone done this before or have suggestions?

Thanks!

Tags (3)
1 Solution

Accepted Solutions
by
MVP Esteemed Contributor

Here's one way to do it

``````var theDate = Date();
var testDate = Date(2022,7,4);

var theMonth = Month(theDate); //note Month is zero-based (Jan = 0)
var theYear = Year(theDate);
var quarter;
when (theMonth < 3, quarter = 1,
theMonth < 6, quarter = 2,
theMonth < 9, quarter = 3,
quarter = 4);

var testMonth = Month(testDate);
var testYear = Year(testDate);
var testquarter;
when (testMonth < 3, testquarter = 1,
testMonth < 6, testquarter = 2,
testMonth < 9, testquarter = 3,
testquarter = 4);

var output = "In current quarter";
if (quarter != testquarter || theYear != testYear) output = "Not in current quarter";
return output;``````

4 Replies
by
MVP Esteemed Contributor

Here's one way to do it

``````var theDate = Date();
var testDate = Date(2022,7,4);

var theMonth = Month(theDate); //note Month is zero-based (Jan = 0)
var theYear = Year(theDate);
var quarter;
when (theMonth < 3, quarter = 1,
theMonth < 6, quarter = 2,
theMonth < 9, quarter = 3,
quarter = 4);

var testMonth = Month(testDate);
var testYear = Year(testDate);
var testquarter;
when (testMonth < 3, testquarter = 1,
testMonth < 6, testquarter = 2,
testMonth < 9, testquarter = 3,
testquarter = 4);

var output = "In current quarter";
if (quarter != testquarter || theYear != testYear) output = "Not in current quarter";
return output;``````

by
MVP Honored Contributor

There isn't a function to directly pull out the quarter, but you could make your own using the month number easily enough. Assuming you already have a way of pulling your inspection date figured out, the rest of it could look like this:

``````// return quarter for a given date
function Quarter(d){
var month_num = Month(d)

return When(
month_num < 3, 1,
month_num < 6, 2,
month_num < 9, 3,
4
)
}

// get your inspection date; i'm using a prior date as a placeholder
var insp = Date(2022,7,13)

// get quarter and year for inspection
var i_qtr = Quarter(insp)
var i_yr = Year(insp)

Console(`Inspection occurred Q\${i_qtr} of \${i_yr}.`)

// get current quarter and year
var curr_qtr = Quarter(Now())
var curr_yr = Year(Now())

// return True if inspection is in current year and quarter
i_qtr == curr_qtr && i_yr == curr_yr``````

Using a placeholder of sometime last month, I get a true. Changing that to sometime in February, it switches to false.

Oh and here's the console output, just to show what that's doing:

- Josh Carlson
Kendall County GIS
New Contributor III

Worked great in my web map!  Now, is it possible to use or calculate that "current quarter" information in a Dashboard gauge or indicator?  Thanks!!

New Contributor III

It turns out that KenBuja's code needed a small change to the When functions.

```var quarter = when (theMonth < 3, 1,
theMonth < 6, 2,
theMonth < 9, 3,
theMonth < 12, 4,
"Fail");```