Converting date to string and combining fields using Python

686
4
Jump to solution
07-06-2018 01:10 PM
by Anonymous User
Not applicable

I'm trying to get more familiar with ArcGIS Pro and by necessity, Python. I have multiple feature classes that are assigned a unique identifier that combines the location and date/time that the data were collected in the format "PARK_YYYYMMDD_HHMMSS". "PARK" comes from a data field [Park_Unit] with a coded domain and date/time from a date field [TRT_BEG_TM]. I'm trying to get it to work using Python but I can't wrap my head around functions and syntax. Here's the VB Script expression I've been using in ArcMap for reference. Thanks in advance.

Pre-Logic Script Code:

strYear= Year( [TRT_BEG_TM] )
strMonth= Month ( [TRT_BEG_TM] )
strDay = Day( [TRT_BEG_TM] )
strHour = Hour( [TRT_BEG_TM] )
strMin = Minute( [TRT_BEG_TM] )
strSec = Second( [TRT_BEG_TM] )
If strDay < 10 Then
  strDay = "0"& strDay
end if
  If strMonth < 10 Then
strMonth = "0" & strMonth
end if
If strHour < 10 Then
  strHour = "0" & strHour
end if
If strMin < 10 Then
  strMin = "0" & strMin
end if
If strSec < 10 Then
  strSec = "0" & strSec
end if
Formatteddate= strYear & strMonth & strDay & strHour & strMin & strSec

LOC_NM=

[PARK_UNIT] &"_"& left(Formatteddate,8) & "_" & right(Formatteddate,6)

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

Forgot you were using ArcGIS Pro, the code above was built on ArcMap.  I will take a look at what has changed with Pro and see if I can tweak the code.

UPDATE:  Good news, Esri actually fixed the field calculator so it returns actual datetime objects instead of string representations of them.  The code is simpler with Pro:

"{}_{:%Y%m%d_%H%M%S}".format( !PARK_UNIT! ,!TRT_BEG_TM!)

View solution in original post

4 Replies
JoshuaBixby
MVP Esteemed Contributor

Try this in the expression box of field calculator:

"{}_{:%Y%m%d_%H%M%S}".format( !PARK_UNIT! ,arcpy.time.ParseDateTimeString( !TRT_BEG_TM!))‍‍

Unfortunately, and for reasons I don't understand, the field calculator returns datetimes as strings, which requires converting them back to datetime using arcpy.time if you want to use native Python datetime handling.

0 Kudos
by Anonymous User
Not applicable

Hi Joshua, that expression returns the following error:

ERROR 000539: Traceback (most recent call last):
File "<expression>", line 1, in <module>
File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\time.py", line 300, in ParseDateTimeString
raise ValueError(datetime_string)
ValueError: 2018-06-28 15:27:48

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Forgot you were using ArcGIS Pro, the code above was built on ArcMap.  I will take a look at what has changed with Pro and see if I can tweak the code.

UPDATE:  Good news, Esri actually fixed the field calculator so it returns actual datetime objects instead of string representations of them.  The code is simpler with Pro:

"{}_{:%Y%m%d_%H%M%S}".format( !PARK_UNIT! ,!TRT_BEG_TM!)
by Anonymous User
Not applicable

Fantastic, that worked, thanks very much.

0 Kudos