Looking for a sample ArcPy script I can modify for my use. Any suggestions?
Typo on my part, an extra minus in days=180 (updated original code):
date_past <SPAN class="operator token">=</SPAN> datetime<SPAN class="punctuation token">.</SPAN>now<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">-</SPAN> timedelta<SPAN class="punctuation token">(</SPAN>days<SPAN class="operator token">=</SPAN><SPAN class="number token">180</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
Perhaps (you could remove time from formatting, if desired):
<SPAN class="keyword token">from</SPAN> datetime <SPAN class="keyword token">import</SPAN> datetime<SPAN class="punctuation token">,</SPAN> timedelta date_past <SPAN class="operator token">=</SPAN> datetime<SPAN class="punctuation token">.</SPAN>now<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">-</SPAN> timedelta<SPAN class="punctuation token">(</SPAN>days<SPAN class="operator token">=</SPAN><SPAN class="number token">180</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># you can add/subtract as required</SPAN> wc <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Field IS NULL AND DateField < DATE '{}'"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>date_past<SPAN class="punctuation token">.</SPAN>strftime<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"%Y-%m-%d %H:%M:%S"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN> wc <SPAN class="comment token"># Field IS NULL AND DateField < DATE '2018-04-08 09:25:35'</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
The month and day are correct but the year is 2019 instead of 2018?
Awesome! Thanks for your help on this.
I used the code you supplied but noticed it's returning everything 180 days or older. I only want results that fall exactly 180 days from current date.
arcpy<SPAN class="punctuation token">.</SPAN>SelectLayerByAttribute_management<SPAN class="punctuation token">(</SPAN>permits<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"NEW_SELECTION"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Date_Accepted = date '{}' AND Fee_Paid Is NULL"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>notice<SPAN class="punctuation token">.</SPAN>strftime<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"%Y/%m/%d"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
You could try 'Between' and set hours = '00:00:00' and '23:59:59' (between will get both these values and all those inbetween). Or add another day with timedelta.
dp = date_past.strftime("%Y-%m-%d") wc = "Field IS NULL AND DateField BETWEEN DATE '{} 00:00:00' AND DATE '{} 23:59:59' ".format(dp,dp)) <SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Membros conectados podem postar, seguir atualizações e mais. Novo aqui? Registre uma conta gratuita.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.