Select to view content in your preferred language

Data Expression returns blank from DateDiff method

126
1
3 weeks ago
AshneyDaniel
Emerging Contributor

I'm creating an Arcade expression to feed a dashboard list. The expression groups on an ID to find the earliest date, then I want to find the difference between that date and todays date but DateDiff is returning blanks. I know that date formatting is picky in arcade. Its probably a formatting issue but I cannot figure it out. 

Here is the code, which successfully returns a featureSet with a 'scheduled_date' and 'today_date' but 'days_remaining' is empty. I've exclude the connection steps since I know I'm successfully pulling in scheduled_date from the source.

 

// Group on area_id and get aggregate values
var groupedRequests = GroupBy(hydrant_requests, ['area_id'], 
    [ 
 {name: 'ScheduledDate', expression: 'schedule_date', statistic: 'MIN'}
     ]
     ); 

var resultFeatures = [];
var feat
var today_date = Today()

// create a feature array which combines the grouped values + a calced field
for (var record in groupedRequests) 
    {
      feat = {
        attributes:
                {
                 'area_id': record['area_id'],
                 'scheduled_date': Number(record['ScheduledDate']),
                 'today_date' : Number(today_date),
                 'days_remaining': DateDiff(Number(record['ScheduledDate']), Number(today_date), 'days')
                }
              }
      
      // Add each feat result to the resultFeatures array 
      Push(resultFeatures, feat); 
     };
    
// Create a dictionary from the feature array
var myDict  = { 
                'fields': [
                    { name: 'area_id', type: 'esriFieldTypeString' },
                    { name: 'scheduled_date', type: 'esriFieldTypeDate' },
                    { name: 'today_date', type: 'esriFieldTypeDate'},
                    { name: 'days_remaining', type: 'esriFieldTypeInteger'} 
                           ],
                'geometryType': '',
                'features': resultFeatures
              };
              
// Convert dictionary to feature set which can be used in the widget
var fs_dict = FeatureSet(Text(myDict));
return fs_dict;

 

 

Result:

AshneyDaniel_0-1735078704482.png

Any ideas appreciated. Thanks!

 

 

 

0 Kudos
1 Reply
jcarlson
MVP Esteemed Contributor

Wrapping dates in Number() is a good idea for Data Expressions, but the function DateDiff should be given two dates. The way you have it written, you're giving it two numbers. Try just taking the number functions out.

'days_remaining': DateDiff(record['ScheduledDate'], today_date, 'days')

 

- Josh Carlson
Kendall County GIS