Hi,
I'm trying to convert a dictionary to a FeatureSet in an Arcade data Expression. Everything works well until I use a Date field.
The following works ok, just inserting null values for the date field
var data_dict = {
'fields': [
{'name': 'EditDate','type': 'esriFieldTypeDate'},
{'name': 'country','type': 'esriFieldTypeString'},
{'name': 'status','type': 'esriFieldTypeInteger'}
],
'geometryType':'',
'features': []
};
for (var i=0; i < 10;i++) {
data_dict.features[i] = {
'attributes': {
'EditDate': null,
'country': 'NL',
'status': 230
}
}
}
Console(data_dict)
var fs = FeatureSet(Text(data_dict))
return fs
It results in a FeatureSet with empty values for the date field and all other field filled.
However, if I try Now() or Text(Now(),"YYYY-MM-DD") or Timestamp() or just a date like string '2021-04-14' :
var data_dict = {
'fields': [
{'name': 'EditDate','type': 'esriFieldTypeDate'},
{'name': 'country','type': 'esriFieldTypeString'},
{'name': 'status','type': 'esriFieldTypeInteger'}
],
'geometryType':'',
'features': []
};
for (var i=0; i < 10;i++) {
data_dict.features[i] = {
'attributes': {
'EditDate': Now(),
'country': 'NL',
'status': 230
}
}
}
Console(data_dict)
var fs = FeatureSet(Text(data_dict))
return fs
I end up with an empty Featureset
Any ideas?
Regards,
Marc
Solved! Go to Solution.
Hi @MarcHoogerwerf_TAP - the date field is looking for a UNIX timestamp, which you can create in Arcade with DateDiff().
var start = ToLocal(Date(1970, 0, 01, 0, 0, 0, 0))
var dt = DateDiff(Now(), start, 'milliseconds')
Hope this helps!
-Peter
Hi @MarcHoogerwerf_TAP - the date field is looking for a UNIX timestamp, which you can create in Arcade with DateDiff().
var start = ToLocal(Date(1970, 0, 01, 0, 0, 0, 0))
var dt = DateDiff(Now(), start, 'milliseconds')
Hope this helps!
-Peter
Hi Peter,
Brilliant! It works now. Thanks
- Marc
@Anonymous User was scratching my head for a while on this, after a quick google it led me here - thanks for your solution.
This is great, i have been struggling with this for over a day. Thanks for the help.
Hi @Anonymous User, I'm tying to figure out where I would assign the "dt" variable within my dictionary. See Expression below. I'm having the same issue, in which the "CloseOutDate" field is not populating.
var portal = Portal("https://cms.maps.arcgis.com/");
var TandJSoft = FeatureSetByPortalItem(portal,"c6366ef0c8424130a76e9e9d08f292de",0,["*"],false);
var JWSoft = FeatureSetByPortalItem(portal,"92497f50542248dcb798d091dd18d004",0,["*"],false);
var start = ToLocal(Date(1970,0,01,0,0,0,0))
var dt = DateDiff(Now(), start, 'milliseconds')
var combinedDict = {
'fields': [
{ name: "Contractor", type:"esriFieldTypeString" },
{ name: "Total", type: "esriFieldTypeDouble" },
{ name: "Address", type: "esriFieldTypeString" },
{ name: "ProjectName", type: "esriFieldTypeString" },
{name: "Status", type: "esriFieldTypeString" },
{name: "CloseOutDate", type: "esriFieldTypeDate" },
],
geometryType: "",
features: [],
};
var i = 0;
// Loop through each FeatureSet and store its attributes
for (var t in TandJSoft) {
combinedDict.features[i++] = {
attributes: {
Contractor: t["Contractor"],
Total: t["Total"],
Address: t["Address"],
ProjectName: t["ProjectName"],
Status: t["Status"],
CloseOutDate: t["CloseOutDate"],
},
};
}
for (var m in JWSoft) {
combinedDict.features[i++] = {
attributes: {
Contractor: m["Contractor"],
Total: m["Total"],
Address: m["Address"],
ProjectName: m["ProjectName"],
Status: m["Status"],
CloseOutDate: m["CloseOutDate"],
},
};
}
// Return dictionary cast as a FeatureSet
return FeatureSet(Text(combinedDict));
@Anonymous User you can remove the variable line 'var dt = ...' completely as you're not wanting to get the date diff for Now(), as that's the current date at time of execution, instead you'll use your field value date to replace the Now(). For each of the date fields, in each featureset update them to the following but change the feature variable to be relevant i.e. t or m:
CloseOutDate: DateDiff(t["CloseOutDate"], start, 'milliseconds')
That did the trick! Thanks so much for the help.
Hi @Anonymous User what's the method for using an existing date field, coming from an exisitng FeatureSet, in a dictionary i.e. @Anonymous User's post below?
Having to do a DateDiff for each date field seems overly complicated. I'm guessing there are some smarts whereby the date field is automatically being converted to text on execution, instead of an epoch as the example from this page (below) https://developers.arcgis.com/arcade/function-reference/date_functions/#date doesn't work:
var recordDate = Date($feature.dateField)
Thanks
This was a huge help, the problem I was then running into was in using data from Survey123 it did not have the time zone correct and I could not figure out why.
I ended up using Number() around my date and it worked out, but I'm curious about the time zone issues I had with DateDiff. Even with Now() it is off by -4 hours, I'm in ADT (-3) which is (-4) when AST. Survey123 is right but the dashboard seems to compensate a second time?