Select to view content in your preferred language

Date field to Text field automatically

502
2
01-13-2023 11:46 AM
Labels (1)
RudyJamet
Emerging Contributor

I would like to automatically convert data from a survey populating a Created_Date field (ex: 10/10/2022 5:37:08 PM) after submission, into a Created_Date_Text field (ex: 20221010).

The geoprocessing tool Convert Time Field works fine but only on a one-time use basis. The text field is not updated when more data are added to the layer. I tried to schedule a recurrent Run but it's not working.

So I was wondering if someone would know of a Arcade/Python script to make the conversion automatically that I could insert into the Calculate Field tool for the text field.

Thanks.

 

0 Kudos
2 Replies
DavidSolari
Frequent Contributor

The "strftime" method on Python datetime objects is the go-to way to get a stable text version of that data. Here's an example for Calculate Field:

!Created_Date!.strftime("%Y-%m-%d %H:%M:%S")

 For a full list of format codes check out https://strftime.org. If you want true live updates you'll probably want to look into Attribute Rules, here's an example for Arcade:

Text($feature["Created_date"], "Y-MM-DD HH:mm:ss")

The reference for this is https://developers.arcgis.com/arcade/function-reference/text_functions/#text. There's disagreements in how the format codes work between the reference and the sample code so play around with the various opstions.

RudyJamet
Emerging Contributor

Thanks for your input David. I'll look into it.

0 Kudos