-2

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):  
Karl
  • 1,664
  • 2
  • 12
  • 19
PythonPerson
  • 11
  • 2
  • 6

2 Answers2

1

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
EikeMike
  • 280
  • 3
  • 12
Karl
  • 1,664
  • 2
  • 12
  • 19
  • I have a code similar to that but my main struggle is how to call the date from each feature class's field (either "Date", "Sample_Date", or "T0_Date" depending on what the field collector decided to name the field) and make that date the one that is being converted into the day of the year. I also forgot to mention this but I am trying to add the day of year that I receive from the code into a empty field that I created called "DOY". Basically if each feature class has 30 samples within them I want to get the sample date of each sample and output the day of year into the DOY field. – PythonPerson Aug 08 '18 at 20:17
  • Basically, how do I get the date in the sample date field in each feature class to become the "test_str" in the code that you have above. – PythonPerson Aug 08 '18 at 20:19
  • It's hard to really help without seeing your code or the data. But from a high level view you probably just need to loop through all your features, try to extract the date field from each one using any of the possible field names, and then use the above code to convert it to the day of the year. – Karl Aug 08 '18 at 20:25
  • I agree with @Karl, if you added some of your code to the question maybe we could help you but without it is not really possible. From what I understand though I would have some if statements that check which of those three fields has a date as you're looping through them. – Raphael Facredyn Aug 08 '18 at 21:05
  • Ignore the code above I am having trouble uploading my code in a way that is organized but basically where I am at is trying to call the date string from the field called "Date" so that I can then code to get the day of year from that string, using the code that y'all have provided . If you know a code that will actually get the dates out of the "Date" field so that I can then use y'alls code to get the day of year that is what I am looking for and should solve my problem. – PythonPerson Aug 09 '18 at 18:46
  • You should edit your post and put the code in there, or just create a new question since it seems like you are needing help with a different problem now. – Karl Aug 09 '18 at 18:56
  • Just added what I have so far. I need the code for "if "Date" exists as one of the fields, use those dates to get the day of year and then add the day of year into the respective "DOY" column". – PythonPerson Aug 09 '18 at 19:08
  • @PythonPerson I added an example of how you would get the date string based on which field exists. I am not experienced in argis, but you would replace `.getValue` with whatever function is required to get the value based on the field name. – Karl Aug 09 '18 at 19:45
0

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)
  • I have a code similar to that but my main struggle is how to call the date from each feature class's field (either "Date", "Sample_Date", or "T0_Date" depending on what the field collector decided to name the field) and make that date the one that is being converted into the day of the year. I also forgot to mention this but I am trying to add the day of year that I receive from the code into a empty field that I created called "DOY". Basically if each feature class has 30 samples within them I want to get the sample date of each sample and output the day of year into the DOY field. – PythonPerson Aug 08 '18 at 20:17
  • Basically, how do I get the date in the sample date field in each feature class to become the "date_string" in the code that you have above – PythonPerson Aug 08 '18 at 20:19