How to maintain date data in arcade expressions

1358
4
Jump to solution
12-14-2021 01:15 AM
Merlin
by
New Contributor III

Hi,

allready some time ago I noticed that arcade wouldn't allow me to keep my exisiting date data after casting it into a dictionary. It was not possible to simply define the corresponding field in the dictionary as esriFieldTypeDate and then send my exisiting dates into that dictionary by using a for-loop.

As an example that didn't produce the desired results, but kept at least the existing date data:

 

var dict = {
fields: [
{ name: 'start_time', type: 'esriFieldTypeString' },
{ name: 'BP2_compost_adoption_yn', type: 'esriFieldTypeString' },
],
geometryType: '',
features: [],
};

var index = 0;

for (var feature in fv) {
dict.features[index] = {
'attributes': {
'start_time': Text(feature['start_time']),
'BP2_compost_adoption_yn': Text(feature['BP2_compost_adoption_yn'])
}}
index++;} 

 

Casting the date data into a string had the crucial disadvantage of not being able to filter that data in the dashboards anymore.

Unfortunatly the arcade documentation about dates didn't tell how to cast dates into UNIX epoch time, which is shown as one input for the date function, and seems to be the neccessary input for the esriFieldTypeDate in a dictionary.

---
0 Kudos
1 Solution

Accepted Solutions
Merlin
by
New Contributor III

 

tl;dr: use the number() function in the for-loop to cast existing date data into UNIX epoch time, this can be read by the esriFieldTypeDate as correct date.

 

By trial and error I ended up trying the number() function on a date, which produces the UNIX epoch time, which is further down the line accepted by the esriFieldTypeDate in a dictionary.

I guess if there would have been an example showing this capabillity of the number() function in the documentation I would have found this solution way faster (google doesnt bring up anything useful either).
Maybe this could be inculded @DerekLaw ?

Well, here is an complete example of how to use the number() function in a way that keeps your exisiting date data alive:

 

var p = 'https://xxx';
var itemID = 'xxx';
var layerID = 0;

var fv = FeatureSetByPortalItem(Portal(p),itemID,layerID,
    ['start_time',
    'BP3_weed_adoption_yn'], 
    false );
    
var dict = {
  fields: [
    { name: 'start_time',           type: 'esriFieldTypeDate' },
    { name: 'BP3_weed_adoption_yn', type: 'esriFieldTypeString' },
  ],
  geometryType: '',
  features: [],
};

var index = 0; 

for (var feature in fv) { 
    dict.features[index] = { 
        'attributes': { 
           'start_time': Number(feature['start_time']), 
           'BP3_weed_adoption_yn': Text(feature['BP3_weed_adoption_yn'])
        }} 
    index++;} 
var fs_dict = FeatureSet(Text(dict));

return fs_dict

 

---

View solution in original post

4 Replies
Merlin
by
New Contributor III

 

tl;dr: use the number() function in the for-loop to cast existing date data into UNIX epoch time, this can be read by the esriFieldTypeDate as correct date.

 

By trial and error I ended up trying the number() function on a date, which produces the UNIX epoch time, which is further down the line accepted by the esriFieldTypeDate in a dictionary.

I guess if there would have been an example showing this capabillity of the number() function in the documentation I would have found this solution way faster (google doesnt bring up anything useful either).
Maybe this could be inculded @DerekLaw ?

Well, here is an complete example of how to use the number() function in a way that keeps your exisiting date data alive:

 

var p = 'https://xxx';
var itemID = 'xxx';
var layerID = 0;

var fv = FeatureSetByPortalItem(Portal(p),itemID,layerID,
    ['start_time',
    'BP3_weed_adoption_yn'], 
    false );
    
var dict = {
  fields: [
    { name: 'start_time',           type: 'esriFieldTypeDate' },
    { name: 'BP3_weed_adoption_yn', type: 'esriFieldTypeString' },
  ],
  geometryType: '',
  features: [],
};

var index = 0; 

for (var feature in fv) { 
    dict.features[index] = { 
        'attributes': { 
           'start_time': Number(feature['start_time']), 
           'BP3_weed_adoption_yn': Text(feature['BP3_weed_adoption_yn'])
        }} 
    index++;} 
var fs_dict = FeatureSet(Text(dict));

return fs_dict

 

---
SayedWali
New Contributor III

Good tip! Exactly what I was after.

0 Kudos
DavidNyenhuis1
Esri Contributor

With this week's update to Arcade and ArcGIS Dashboards, date fields in feature set constructors now just work. You no longer have to wrap dates with Number() if you pass the dictionary into the FeatureSet() function (which as of this release accepts a dictionary as opposed to only text based JSON).

Instead of:

return FeatureSet(Text(dict))

Do this:

return FeatureSet(dict)

Learn more in this blog post

NOTE: For Enterprise users, this update is targeted for 11.2

SayedWali
New Contributor III

Yep, it worked, thank you!

0 Kudos