Arcade Expression – Check if Current Date is Within Two Dates

arcadearcgis-onlinedatelabelingsymbology

I am new to building expressions in Arcade and I am having trouble writing an expression that can be used to check start and end times of different areas and evaluate if the current date is within the start and end time for each area. Ideally the expression would check the current time and symbolize each area based on if it is within the start and end times or not.

My layer has both a start time and end time feature in Date format. I would like to symbolize each area as either 'opened' or 'closed' based on pre set start and end times.

Here is what I have come up with so far that doesn't quite work as I'd like:

//Expression that evaluates the status of crab catch areas

//returns open if current time is within start and end times

//returns closed if current time is outside of start and end times

var started = $feature.StartTime;

var present = Now()

IIf(started < present, 'open', 'closed')

var closed = $feature.EndTime;

IIf(closed > present, 'open', 'closed')

Right now it will not correctly symbolize the layer based on given start and end times.

Does anyone know where I messed up or another way to go about making a custom expression for this task?

This is for a map created and managed on ArcGIS Online.

Best Answer

Try this

 var started = $feature.StartTime
    var closed = $feature.EndTime
var present = Now()


// if current date/time is greater than or equal to the start time
// AND current date/time is less than or equal to the close time 
// return open, otherwise its closed
If (present >= started && present <= closed) {
    return "Open"
} else {
    return "Closed"
}
Related Question