I'm using the following to get a list of dates in a field called "EFF_DATE" from an attribute table for some pre-selected features.

selectDates = [row[0] for row in arcpy.da.SearchCursor(selects, "EFF_DATE")]

I then use max() to get the most current date from that list:

selectDates = max(selectDates)

This is my result for In[]:selectDates

datetime.datetime(2012, 12, 18, 0, 0)

If I run In[]:print(selectDates), I get

2012-12-18 00:00:00 That looks better but what I want is just this '12/18/2012' How do I convert it?


While the original poster's provided solution of individually selecting the day month and year and constructing a string from it is a valid method of accomplishing the goal, the significantly shorter and easier method (in my opinion), is to use the datetime module's strftime() method (https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior).

so it may look something like:

import datetime
... <your code here> ...
eff_date = selectDates.strftime('%m/%d%Y')
... <continue your code>...

And using the various formatting options described above, you could make a date/time string look however you wanted it to read. Ex: with .strftime('%A, %B %d, %Y') you could have it output in the format 'Wednesday, September 12, 2018'

| improve this answer | |

OK, I was able to resolve this after a little research. Basically, datetime.datetime formats results by day, month, year, seconds, minutes, hour, etc... All I have to do was pull out the str(value) I wanted and add a "/". The result was something like this:

eff_date = str(selectDates.month) + "/" + str(selectDates.day) + "/" + str(selectDates.year)

This resulted in: eff_date = '12/18/2018'

| improve this answer | |

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.