Select to view content in your preferred language

Date Calculation in Arcade

457
2
Jump to solution
04-20-2023 01:50 PM
Labels (1)
JillianStanford
Occasional Contributor III

Hi,

In a data expression, I'm filtering a feature layer of 911 calls to those in the last 24 hours. The filter often returns more records than I think it should

Here is the Arcade:

var portal = Portal("https://arcgis.com"); 
var accidents = FeatureSetByPortalItem(portal, "********",1, ['*'], true)
var t1 = Text(Now(),'Y-MM-DD HH:mm')
var t2 = Text(DateAdd(t1, -24, 'hours'),'Y-MM-DD HH:mm')
var sql = "call > @t2"
Console("call > " + t2)
var recent_accidents = Filter(accidents, sql)

for (var s in recent_accidents) {
  Console(Text(s.call,'Y-MM-DD HH:mm'))
}

 

Here are the results:

JillianStanford_0-1682023329551.png

I can't figure out why the call at 14:02 is not being filtered out, even though it occurs before 15:33.

Can someone point out my error here?

Thanks!

Jill

 

0 Kudos
1 Solution

Accepted Solutions
DavidPike
MVP Frequent Contributor

Even with the comparison being done on text forms of the datetime, the formatting should allow for the comparison to work as intended.  I'm wondering if timezones are a factor here and ToUTC() should be implemented.

var portal = Portal("https://arcgis.com"); 
var accidents = FeatureSetByPortalItem(portal, "********",1, ['*'], true)
var t1 = Text(ToUTC(Now()),'Y-MM-DD HH:mm')
var t2 = Text(DateAdd(t1, -24, 'hours'),'Y-MM-DD HH:mm')
var sql = "call > @t2"
Console("call > " + t2)
var recent_accidents = Filter(accidents, sql)

for (var s in recent_accidents) {
  Console(Text(ToUTC(s.call),'Y-MM-DD HH:mm'))

View solution in original post

0 Kudos
2 Replies
DavidPike
MVP Frequent Contributor

Even with the comparison being done on text forms of the datetime, the formatting should allow for the comparison to work as intended.  I'm wondering if timezones are a factor here and ToUTC() should be implemented.

var portal = Portal("https://arcgis.com"); 
var accidents = FeatureSetByPortalItem(portal, "********",1, ['*'], true)
var t1 = Text(ToUTC(Now()),'Y-MM-DD HH:mm')
var t2 = Text(DateAdd(t1, -24, 'hours'),'Y-MM-DD HH:mm')
var sql = "call > @t2"
Console("call > " + t2)
var recent_accidents = Filter(accidents, sql)

for (var s in recent_accidents) {
  Console(Text(ToUTC(s.call),'Y-MM-DD HH:mm'))
0 Kudos
JillianStanford
Occasional Contributor III

That seems to have done the trick! Thank you!

0 Kudos