[GIS] Changing Format of Date Field in ArcMap Field Calculator

arcgis-10.3arcgis-desktopdatefield-calculatorpython-parser

I am trying to change the format of date field using ArcMap field calculator.

My data is currently in the format "%d/%m/%Y %h:%m:%s" (e.g. 5/05/2016 12:25:46 AM)

I want to change this to "%d/%m/%Y" (e.g. 5/05/2016). No time data.

I have been trying to use the following, but this does not work.

datetime.strptime( !DateField!, "%d/%m/%Y)

I have looked at several similar questions posted, but none seem to do this?

Python or VBScript is ok, using ArcGIS 10.3

Best Answer

The dates are stored as strings (to be precise, unicode) meaning that you need to construct a true Python datetime object and then manipulate it.

If my date field MyDate is 2017-08-09 08:51:32, then I'd use this snippet

str(datetime.datetime.strptime(!MyDate!, "%Y-%m-%d %H:%M:%S").date()) to get 2017-08-09.

If your date is 5/05/2016 12:25:46 AM, you would need to use

str(datetime.datetime.strptime(s, "%d/%m/%Y %H:%M:%S %p").date()) to get 5/05/2016

Related Question