Select to view content in your preferred language

Converting a string into date format

993
3
05-09-2023 12:47 AM
KlaraHonzak
New Contributor

Hello, I want to use ArcGIS Pro (version 3.1.0) to create a space time cube, but I am having problems converting the field with the datetime into date format. The tool "Convert Time Field" only converted the date, but left out the time (more specifically the string '20220617010000' in the format 'yyyyMMddHHmmss' was converted to '6/7/2022'. Different formats of the datatime also haven't worked. I also tried using the tool "Calculate Field", but with this I also only managed to convert the date without the time. Does anyone know how to solve this problem?

0 Kudos
3 Replies
DavidPike
MVP Frequent Contributor

Datetime strptime module will cover this for you.  I've pinched the syntax from googling.

from datetime import datetime

input_string = '20220617010000'

dt_object = datetime.strptime(input_string, '%Y%m%d%H%M%S')
#print(dt_object)
return dt_object
0 Kudos
KlaraHonzak
New Contributor

Yes, I also expected that this would work, but for some reason the resulting field is in text format and not date.

 

0 Kudos
DavidPike
MVP Frequent Contributor

Strange.  Do you have a screenshot to get more of an understanding what may be happening?

The field type is definitely Date?

In the field designer, have you got any custom formatting or Default settings on (default would match your system time format) https://pro.arcgis.com/en/pro-app/latest/help/data/tables/format-numeric-and-date-fields.htm 

I'd set the format to 'None'  - Note times of Midnight will show as having no time

 

from the link:

Date and time formats

Date formats, unlike numeric formats, only have one category: Date. Within this category, you can choose from multiple supported date formats. The default is None, which includes the full date and time, except for midnight when only the date is shown. Optionally, you can choose System Default, the date displayed matches the default established by the operating system, or use Custom to generate your own format string.

0 Kudos