Convert a feature set of dates to years only

802
4
Jump to solution
05-23-2022 04:57 AM
Labels (2)
DaveBodak
Occasional Contributor

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 pm0
09 Feb 2021 11:40:35 am1
09 Feb 2017 12:01:14 pm2
09 Feb 2021 11:40:43 am3
09 Feb 2017 12:01:26 pm4

Like I mentioned, just the year of the date would be great, thanks!

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
jcarlson
MVP Esteemed Contributor

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
- Josh Carlson
Kendall County GIS

View solution in original post

0 Kudos
4 Replies
jcarlson
MVP Esteemed Contributor

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)'
    }
)

 

- Josh Carlson
Kendall County GIS
0 Kudos
DaveBodak
Occasional Contributor

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.

0 Kudos
jcarlson
MVP Esteemed Contributor

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
- Josh Carlson
Kendall County GIS
0 Kudos
DaveBodak
Occasional Contributor

That's perfect, thank you!

0 Kudos