Displaying time spans with Arcade in dashboards

347
1
04-08-2021 06:32 AM
Labels (1)
DEI
by
New Contributor III

Hello!

I copied and slightly modified code from the following Blog: https://www.esri.com/arcgis-blog/products/ops-dashboard/real-time/displaying-time-spans-with-arcade-... (Many thanks to @David Nyenhuis and @Noora Golabi).

As my map gets updated, the seconds displayed start out in reverse like a countdown (54, 48, 42... to 0) and then proceed as expected (6, 12, 18...). The countdown shows up whether I specify type 1 or type 2.

Can anyone please tell me why I'm getting the countdown?

Here's what I'm using:


var dateOpened = $datapoint["last_edited_date"];
var currentTime = Now();
var secondsOpen = DateDiff(currentTime, dateOpened, 'seconds');

function formatElapsedTime(numSeconds, type) {
if (IsEmpty(type)) { type = 1 };
numSeconds = Floor(Abs(numSeconds));
var numMinutes = Floor(numSeconds/60,0);
var numHours = Floor(numSeconds/(60*60),0);
var numDays = Floor(numSeconds/(60*60*24),0);

if (type == 1) {
var elapsedTime = When(
numMinutes < 1, 'This Map was Updated '+ numSeconds + ' Seconds Ago. Click Here to Zoom to Latest Update.',
numMinutes == 1, 'This Map was Updated 1 Minute Ago. Click Here to Zoom to Latest Update.',
numHours < 1, 'This Map was Updated '+ numMinutes + ' Minutes Ago. Click Here to Zoom to Latest Update.',
numHours == 1, 'This Map was Updated 1 Hour Ago. Click Here to Zoom to Latest Update.',
numDays < 1, 'This Map was Updated '+ numHours + ' Hours Ago. Click Here to Zoom to Latest Update.',
numDays == 1, 'This Map was Updated 1 Day Ago. Click Here to Zoom to Latest Update.',
'This Map was Updated '+ numDays +' Days Ago. Click Here to Zoom to Latest Update.',
);
return elapsedTime;
}

if (type == 2) {
var relativeDate = DateAdd(Date(0,0,0,0,0,0), numSeconds, 'seconds');
var elapsedTime = When(
numHours < 1, Text(relativeDate,'mm:ss'),
numDays < 1, Text(relativeDate,'H:mm:ss'),
numDays + ' days ' + Text(relativeDate,'H:mm')
);
return elapsedTime;
}
}


var timeFormatted = formatElapsedTime(secondsOpen, 1);

return {
attributes: {
timeFormatted: timeFormatted
}
}

 

 

 

1 Reply
jcarlson
MVP Frequent Contributor

In your custom function, you're setting the numSeconds to an Abs value, so when your DateDiff returns a negative number, it's flipping it positive.

In the linked post, the authors do this so that the output is simply a duration, and is not concerned with the "direction" of the span. But the way DateDiff functions, given DateDiff(date_a, date_b, units), if date_b is after date_a, you will get a negative duration.

As far as why you're getting a negative duration between "now" and the last edit timestamp, that's a bit of a mystery. The implication is that somehow dateOpened is coming in at a future date. You may need to check on the source of the data to see if it's configured properly for time zone, etc.

- Josh Carlson
Kendall County GIS
0 Kudos