I am having a heck of a time with this in field calculator and I've looked at the threads in geonet on datetime to string but none them seem to work for me. The ultimate goal is that I need to get a value out of datetime as an integer (so month would come through as an integer value of 2 for February for example). Since datetime seems to be formatted as a unicode string in Field Calculator, I think I need to get it to a regular string and then split it on the slashes so I can get the number I want and convert it to an integer before returning it. I have tried so many different solutions I couldn't write them all down here... can someone help me with this?
Date field is "Actual_Completion" and contains values displayed like so: 12/1/2015
(Long) Integer field is "Month" and needs the end value to be like so: 12
The closest I've gotten is this:
int<SPAN class="punctuation token">(</SPAN>str<SPAN class="punctuation token">(</SPAN>!Actual_Completion!<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
and it will return the first value of the month, but that's problematic for returning something like 12.
If I try
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">getMonth</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
x <SPAN class="operator token">=</SPAN> str<SPAN class="punctuation token">(</SPAN>!Actual_Completion!<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>split<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"/"</SPAN><SPAN class="punctuation token">)</SPAN>
x <SPAN class="operator token">=</SPAN> int<SPAN class="punctuation token">(</SPAN>x<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">return</SPAN> x<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
I get an invalid syntax error on line 2.
I've tried many different iterations of this:
<SPAN class="keyword token">from</SPAN> datetime <SPAN class="keyword token">import</SPAN> datetime
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">getMonth</SPAN><SPAN class="punctuation token">(</SPAN>dateObj<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
x <SPAN class="operator token">=</SPAN> datetime<SPAN class="punctuation token">.</SPAN>strftime<SPAN class="punctuation token">(</SPAN>dateObj<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'%m'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">return</SPAN> int<SPAN class="punctuation token">(</SPAN>x<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
I get an error about strftime needing a datetime object and not a unicode object.