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
-
Was this one of the "several similar questions posted" that you looked at? gis.stackexchange.com/questions/165892/…PolyGeo– PolyGeo ♦2017年08月09日 06:41:46 +00:00Commented Aug 9, 2017 at 6:41
2 Answers 2
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
No need for python, just use the Convert Time Field tool!
Explore related questions
See similar questions with these tags.