Select to view content in your preferred language

Using timedelta to add days to a start date

293
2
Jump to solution
06-06-2024 07:25 PM
JasmineSpring
New Contributor III

Hello

I am trying to create a scriptool  to select the survey data after a certain start date

If Annual_Growth is 10 I want it to add 20 days to the  Initial_Date to give the start date

If Annual_Growth is 20 I want it to add 50 days to the  Initial_Date to give the start date

 

 

 

Survey_Data = arcpy.GetParameterAsText(0) 
Annual_Growth = arcpy.GetParameterAsText(1) 
Initial_Date = arcpy.GetParameterAsText(2)  #Date input (date format is dd/mm/yyyy)
T_date = datetime.datetime(Inital_Date)
start_date = T_date + datetime.timedelta(days = num_days)
if Annual_Growth ==10: num_days = 20
elif Annual_Growth == 20: num_days = 50
Where_Clause = "DateTimeUTC >= '{}'".format(start_date)
arcpy.management.SelectLayerByAttribute(Survey_Data, "NEW_SELECTION", Where_Clause)

 

 

 

The first issue I run into is that the initial date input is perhaps in the wrong format for timedelta to add the days ?? Any help would be appreciated. Thank you

0 Kudos
1 Solution

Accepted Solutions
JasmineSpring
New Contributor III

Thanks for reply Tom. Yep thats where I started also. Got there in the end. In a roundabout way.

 

 

 

 

Survey_Data = arcpy.GetParameterAsText(0)     
Date_last_treatment = arcpy.GetParameterAsText(1)
AGrowth = arcpy.GetParameterAsText(2)
Initial_date=datetime.datetime.strptime(Date_last_treatment,'%d/%m/%Y')
Growth = int(AGrowth)
if Growth == 10:
    Ndays = 10
elif Growth == 20:
    Ndays = 20
N_date = Intial_date + datetime.timedelta(days = Ndays)
Start_date= datetime.datetime.strftime(N_date, '%d/%m/%Y')
Start_date1 = "timestamp'{}'".format(Start_date)
Where_Clause = "DateTimeUTC >= {}".format(Start_date1)
arcpy.management.SelectLayerByAttribute(Survey_Data, "NEW_SELECTION", Where_Clause)

 

 

 

 

 

View solution in original post

0 Kudos
2 Replies
Tom_Laue
New Contributor III

A couple things come to mind…

 

  1. When testing, I would add to your script
    1. print(type(Initial_Date))
      1. That way you can tell what date type it is and research how to do a time delta to that format (or if you need to convert it to a different datetime format)
  2. I have seen issues where a date query doesn’t work unless I give it a range of dates.
    1. Date > 6/6/2024
      1.      In some cases that does not work
    2. Date > 6/6/2024 and Date<1/1/2099
      1.      Works when the first one doesn’t
0 Kudos
JasmineSpring
New Contributor III

Thanks for reply Tom. Yep thats where I started also. Got there in the end. In a roundabout way.

 

 

 

 

Survey_Data = arcpy.GetParameterAsText(0)     
Date_last_treatment = arcpy.GetParameterAsText(1)
AGrowth = arcpy.GetParameterAsText(2)
Initial_date=datetime.datetime.strptime(Date_last_treatment,'%d/%m/%Y')
Growth = int(AGrowth)
if Growth == 10:
    Ndays = 10
elif Growth == 20:
    Ndays = 20
N_date = Intial_date + datetime.timedelta(days = Ndays)
Start_date= datetime.datetime.strftime(N_date, '%d/%m/%Y')
Start_date1 = "timestamp'{}'".format(Start_date)
Where_Clause = "DateTimeUTC >= {}".format(Start_date1)
arcpy.management.SelectLayerByAttribute(Survey_Data, "NEW_SELECTION", Where_Clause)

 

 

 

 

 

0 Kudos