Hello,
I am working on a project where I need to filter out the year of many dates in a large feature set using arcade. The current format is the standard YYYY-MM-DD, but I only want to use the year from this format. Is there a way to convert this to a string/integer and use this to pull the date?
Here is the code that I have this far:
//Return populated out_dict as FeatureSet
var dates = FeatureSet(Text(out_dict))
Here is the output:
09 Feb 2017 12:00:00 pm | 0 |
09 Feb 2021 11:40:35 am | 1 |
09 Feb 2017 12:01:14 pm | 2 |
09 Feb 2021 11:40:43 am | 3 |
09 Feb 2017 12:01:26 pm | 4 |
Like I mentioned, just the year of the date would be great, thanks!
Solved! Go to Solution.
Well if we're just working it into this expression, that's easy enough. We just need a new field in the out_dict and then we need to populate it in the for loop.
var fs = FeatureSetByPortalItem(
Portal('https://www.arcgis.com'),
'9e935789e337432a8314f0ede0f9ac7c',
0,
[
'InspectionDate',
],
false
);
// List of inspectors per field per feature; empty for now
var out_dict = {
fields: [
{name: 'inspdate', type: 'esriFieldTypeDate'},
{name: 'inspyear', type: 'esriFieldTypeInteger'}
],
geometryType: '',
features: []
}
// Iterate over each feature in the layer
for (var f in fs){
Push(
out_dict['features'],
{attributes:
{
inspdate: Number(f['InspectionDate']),
inspyear: Year(f['InspectionDate'])
}
}
)
}
//Return populated out_dict as FeatureSet
var dates = FeatureSet(Text(out_dict))
return dates
Where are you trying to implement this expression? Your expression kind of resembles what I'd expect from a Data Expression; is this for a Dashboard?
In any case, to get a FeatureSet full of datetime values to give you the year for each, you could do a number of things. The simplest, I think, would be to use GroupBy. This function lets you use SQL to create new fields in the output FeatureSet, and if you "group" by your unique identifier, your output will have the same number of rows as the input.
Here's a generalized version of what the grouping would look like. More specifics about your situation would help tailor this expression to what you're doing.
return GroupBy(
fs,
'objectid',
{
name: 'the_year',
expression: 'EXTRACT(YEAR FROM date_field)'
}
)
Hi Josh,
Yes, this is for a dashboard use. I believe you had assisted in writing the larger portion of the code for this, which has worked perfect for the other uses.
var fs = FeatureSetByPortalItem(
Portal('https://www.arcgis.com'),
'9e935789e337432a8314f0ede0f9ac7c',
0,
[
'InspectionDate',
],
false
);
// List of inspectors per field per feature; empty for now
var out_dict = {
fields: [{name: 'inspdate', type: 'esriFieldTypeDate'}],
geometryType: '',
features: []
}
// Iterate over each feature in the layer
for (var f in fs){
Push(
out_dict['features'],
{attributes:
{inspdate: Number(f['InspectionDate'])
}
}
)
}
//Return populated out_dict as FeatureSet
var dates = FeatureSet(Text(out_dict))
return dates
In this case, I am receiving the entire date back in table for the hydrant inspections. I am just looking to get the year returned from this section.
Thank you for your help.
Well if we're just working it into this expression, that's easy enough. We just need a new field in the out_dict and then we need to populate it in the for loop.
var fs = FeatureSetByPortalItem(
Portal('https://www.arcgis.com'),
'9e935789e337432a8314f0ede0f9ac7c',
0,
[
'InspectionDate',
],
false
);
// List of inspectors per field per feature; empty for now
var out_dict = {
fields: [
{name: 'inspdate', type: 'esriFieldTypeDate'},
{name: 'inspyear', type: 'esriFieldTypeInteger'}
],
geometryType: '',
features: []
}
// Iterate over each feature in the layer
for (var f in fs){
Push(
out_dict['features'],
{attributes:
{
inspdate: Number(f['InspectionDate']),
inspyear: Year(f['InspectionDate'])
}
}
)
}
//Return populated out_dict as FeatureSet
var dates = FeatureSet(Text(out_dict))
return dates
That's perfect, thank you!