Retrieve Unique Dates From Attribute Field

10-18-2017 12:59 PM
New Contributor III


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:

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

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
MVP Frequent Contributor
formatteddatetime = yourdatevalue.strftime("%Y/%m/%d")
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")