I have a DateTime field (ex: CreatedDate) and would like to only show the date with the label. How would I setup the label expression to do this using python?
It is funny that python interpreter (field calculator) treats Arcgis datetime fields as unicode texts. to extract date from this string, we should convert it to datetime and then extract the date and then convert to string!
Standalone code (assuming you have a datetime formatted like
10/8/2015 3:24:53 AM):
print str(datetime.datetime.strptime(u'10/8/2015 3:24:53 AM',"%m/%d/%Y %H:%M:%S %p").date())
label expression (python parser):
def FindLabel ( [DateModified] ): return str(datetime.datetime.strptime([DateModified],"%m/%d/%Y %H:%M:%S %p").date())
The date in a DateTime field is the first 10 characters of the field.
I tried the following in a field called
In your label expression, set your parser to Python.
Tick the Advanced box and set your expression to the following:
def FindLabel ( [last_edited_date] ): return [last_edited_date][:10]
last_edited_date to whatever your field is called. The key here is to use python's string slicing ability. That is,
[:10] to get the first 10 characters of the field.
The results look like this: