How to maintain date data in arcade expressions

393
2
Jump to solution
12-14-2021 01:15 AM
Labels (1)
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

2 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