0

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?

asked Sep 12, 2018 at 15:00

2 Answers 2

1

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'

answered Sep 12, 2018 at 19:19
0

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'

answered Sep 12, 2018 at 16:09

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.