2

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?

1

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

The date in a DateTime field is the first 10 characters of the field.

I tried the following in a field called last_edited_date.

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]

And change 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.

enter image description here

The results look like this:

enter image description here

1
  • I don't understand the negative vote... It's explained and better it works! I use the same solution. But it's a text in findlabel to local format. So yes 10 first caracters return date without time – GeoStoneMarten Dec 2 '15 at 21:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.