Select to view content in your preferred language

Arcade Find Weekday && Time

988
17
Jump to solution
07-26-2024 07:51 AM
JaredPilbeam2
MVP Regular Contributor

This is a nice snippet I got from an ESRI dev that returns what day a location has open in the Pro Symbology Expression Builder. It uses the DaysOpen field in my layer. My field values are like so: Su,M,Tu,W,Th,F,Sa.

Here is the expression:

var days_abbr = ["Su", "M", "Tu", "W", "Th", "F", "Sa"];
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var weekday = Weekday(Today());

If(Find(days_abbr[weekday],$feature.DaysOpen,0)!=-1) {
  return "Open (" + days[weekday] + ")";
} 
else {
  return "Closed";
}

 

My question here is how to expand on that to include time using the Find() function. I created similar variables to get the time.

Here is one attempt which returns an error (see pseudo text below) on the If() line.

var days_abbr = ["Su", "M", "Tu", "W", "Th", "F", "Sa"];
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var weekday = Weekday(Today());

var hours = ["8","8.5","9","9.5"]; //I also tried ["8","8:30","9","9:30"] which resulted in error
var timeofday = Time(Now()); //this displays the time the way I anticipated it

If(Find(days_abbr[weekday],$feature.DaysOpen,0)!=-1 && Find(hours[timeofday],$feature.Time,0)!=-1) {
  return "Open Now";
} 
else {
  return "Closed";
}

// Error: Invalid expression. Integer index expected.

 

0 Kudos
17 Replies
KenBuja
MVP Esteemed Contributor

The null field shouldn't be a problem, but those other values for Time are confusing. What does something like "9:30, 10, 11, 15:30, 18:30"  or "9, 8, 19, 21" represent?

0 Kudos
JaredPilbeam2
MVP Regular Contributor

Here's the bigger picture. The locations rarely have simple hours. It changes on certain days and on weekends. My logic along the way was to put the starting and end times for each day into the Time field.

JaredPilbeam2_1-1722025071383.png

Edit: I'm more or less experimenting on a working dataset. The LocationHours field is being used in the pop-up currently.

0 Kudos
KenBuja
MVP Esteemed Contributor

I found it almost impossible to figure out how to extract the days and times logically in an Arcade script. The only thing I could come up with it to have a string field (LocationHoursByDay) with each day's time span or closed in a concatenated list, where "M - Tr 9:30 AM - 8 PM, F 9:30 AM - 6:30 PM, Sa - 10 AM - 3:30 PM, Su 11 AM - 3:30 PM"

would be represented as "9:30-20, 9:30-20, 9:30-20, 9:30-20, 9:30-18:30, 10-15:30, 11-15:30"

while the line "M,W,F 8:30 AM - 11 AM"

would be "8:30-11, closed, 8:30-11, closed, 8:30-11, closed, closed"

That field would be utilized in this script

function ConvertTime(input) {
  var theTime = input;
  if (Find(":", theTime) > -1) {
    var times = split(theTime, ":");
    theTime = Number(times[0]) + Number(times[1]) / 60;
  }
  return Number(theTime);
}
var current = ConvertTime(Time(Now()));
var fTime = '8:30-11, closed, 8:30-11, closed, 8:30-11, closed, closed'; //$feature.LocationHoursByDay,
var times = Split(fTime, ', ')
var todaysTime = times[Weekday(Today()) - 1];
if (todaysTime == 'closed') return 'Closed'

var todaysTimes = Split(todaysTime, '-')
var start = ConvertTime(todaysTimes[0]);
var end = ConvertTime(todaysTimes[1]);

iif(current >= start && current <= end, 'Open Now', 'Closed')

 

JaredPilbeam2
MVP Regular Contributor

Thanks for the code. Unless I'm doing something wrong, I'm only seeing two Closed symbols created (highlighted in table below). And what's odd is that when when you zoom in Closed symbols for all features appear. There are no Open Now symbols created.

On what's your line 10 I used this variable:

 

var fTime = $feature.LocationHoursByDay

 

I created a text field called LocationHoursByDay.

JaredPilbeam2_1-1722280830925.png

0 Kudos
KenBuja
MVP Esteemed Contributor

I used a testing layer to try this out and it does show different symbols. I put that same code in the Label expression for more testing.

Snag_1cd80f5.png

0 Kudos
JaredPilbeam2
MVP Regular Contributor

I see what I did wrong. You have a space after the comma in the string here. In my table I didn't have spaces. I removed it in the script and it worked! Thanks a lot.

var times = Split(fTime, ',')

 

But I'm still getting weird behavior with what appears to be scale. Most features aren't visible until I zoom in. And I do not have the layer set with zoom in/out values. I'll have to investigate.

openclosed.gif

0 Kudos
KenBuja
MVP Esteemed Contributor

Is there a scale restriction on the layer? Check in the layer properties to see if the layer is showing at all scales.

And you can always change the split function to take into account a space after the comma.

var times = Split(fTime, ', ')

 

 

0 Kudos
JaredPilbeam2
MVP Regular Contributor
0 Kudos