I currently have multiple feature classes in ArcGIS that all contain a field either called Date
, Sample_Date
, or T0_Date
that has the date each sample was taken. The dates do not contain leading zeros (ex. 7/4/2014
not 07/04/2014
).
I need a code in python or arcpy that will convert the sample date in the field to its corresponding day of the year (leap year mindful)(January 1st being 1 and December 31 being 365).
I have seen many codes using datetime and strptime which seems to be the road I need to take but most of the codes that I have seen require the user to enter the specific day they are looking for but since I have so many data files I was looking for a code that would automate it for me. Please let me know of any codes that you know of that would help me do this.
import arcpy
import datetime
from datetime import datetime
fcList = arcpy.ListFeatureClasses()
for fc in fcList:
if "DOY" not in arcpy.ListFields(fc):
arcpy.AddField_management(fc,"DOY","SHORT")
if "DOY" in arcpy.ListFields(fc):
if "Date" in arcpy.ListFields(fc):
You could use the datetime module. Assuming your dates are in Month/Day/Year: