[GIS] Convert DateTime field to Just Date in Label Expression

arcgis-desktopdatetimelabelingpython

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?

Best Answer

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())