Retrieve Unique Dates From Attribute Field

173
2
10-18-2017 12:59 PM
MikeEdwards
New Contributor III

Hello,

I'm trying to pull all of the unique dates out of an attribute field and I'm having issues formatting the output. Right now I have the following:

table = r"C:\Temp\SomeFile.shp"
field = "ADD_DATE"
valueList = []
rows = arcpy.SearchCursor(table)

   for row in rows:
      valueList.append(row.getValue(field))

uniqueSet = set(valueList)
uniqueList = list(uniqueSet)
uniqueList.sort()


del rows
del row

print uniqueList

The output is datetime.datetime(2017, 9, 19, 14, 28, 29) I'd like it to simply be 2017/09/19

Can anyone help?

0 Kudos
2 Replies
JamesCrandall
MVP Frequent Contributor
formatteddatetime = yourdatevalue.strftime("%Y/%m/%d")
JoshuaBixby
MVP Esteemed Contributor

You will want to use the newer Data Access cursors:  SearchCursor—Help | ArcGIS Desktop.  Python's Counter class was specifically made for situations like this.

from collections import Counter

table = r"C:\Temp\SomeFile.shp"
field = "ADD_DATE"

cnt = Counter(dt for dt, in arcpy.da.SearchCursor(table,field))
for dt, freq in cnt.items():
    if dt:
        print dt.strftime("%Y/%m/%d")