Converting arcgis date field into day of the year

879 Views Asked by At

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):  
2

There are 2 best solutions below

2
On

You could use the datetime module. Assuming your dates are in Month/Day/Year:

from datetime import datetime

date_string = "7/4/2014"

parts = date_string.split("/")

month = int(parts[0])
day = int(parts[1])
year = int(parts[2])

print(datetime(year, month, day).timetuple().tm_yday)
8
On

This is a pretty simple thing to do using the datetime package. It has a function called strptime which takes in a string and converts it to a datetime. Then there is another function called strftime which will output the datetime in a specific format (in your case the day of the year).

You can look at the documentation for more info about datetimes here.

Example of getting the day of the year:

def get_DOY(date_string):
    # First convert string to datetime object
    parsed_datetime = datetime.datetime.strptime(date_string, '%m/%d/%Y') 

    # Then get day of year from datetime
    day_of_year = parsed_datetime.timetuple().tm_yday
    return day_of_year

Edit: Simpler approach is to get the day of year from a timetuple so that it outputs it directly as an int

I am not experienced in arcgis, but assuming the function is .getValue to get the value based on the field name:

import arcpy 
import datetime  # You should only import datetime once

fcList = arcpy.ListFeatureClasses()  
for fc in fcList:  
    field_list = arcpy.ListFields(fc)
    if "DOY" not in field_list:  
        arcpy.AddField_management(fc,"DOY","SHORT")  
    
    date_string = None

    # Assuming .getValue is how you get a value from the fc
    if "Date" in field_list:
        date_string = fc.getValue("Date")
    elif "Sample_Date" in field_list:
        date_string = fc.getValue("Sample_Date")
    elif "T0_Date" in field_list:
        date_string = fc.getValue("T0_Date")

    if date_string is not None:
        day_of_year = get_DOY(date_string) # call function as listed above
    else:
        # handle when no date value found here