<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Datetime to String or Datetime to Integer in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/datetime-to-string-or-datetime-to-integer/m-p/574066#M44976</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;So while you can't cast the !Actual_Completion! field to a string directly using str() unless it's in an int() function for some reason I don't understand, I encoded the unicode value so it was a plain string and got it to work.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Codeblock:&amp;nbsp;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;def&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;getMonth&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;dateField&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
  dateString &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; dateField&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;encode&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'UTF8'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
  dateParts &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; dateString&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;split&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"/"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
  &lt;SPAN class="keyword token"&gt;return&lt;/SPAN&gt; int&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;dateParts&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;0&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;‍‍‍‍‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 12 Dec 2021 00:42:44 GMT</pubDate>
    <dc:creator>MKF62</dc:creator>
    <dc:date>2021-12-12T00:42:44Z</dc:date>
    <item>
      <title>Datetime to String or Datetime to Integer</title>
      <link>https://community.esri.com/t5/python-questions/datetime-to-string-or-datetime-to-integer/m-p/574065#M44975</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;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?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Date field is "Actual_Completion" and contains values displayed like so: 12/1/2015&lt;/P&gt;&lt;P&gt;(Long) Integer field is "Month" and needs the end value to be like so: 12&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The closest I've gotten is this:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;int&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;str&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;!Actual_Completion!&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;0&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;and it will return the first value of the month, but that's problematic for returning something like 12.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If I try&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;def&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;getMonth&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt; 
  x &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; str&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;!Actual_Completion!&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;split&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"/"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;‍
  x &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; int&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;x&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;0&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
  &lt;SPAN class="keyword token"&gt;return&lt;/SPAN&gt; x‍‍‍‍‍‍‍‍‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I get an invalid syntax error on line 2.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've tried many different iterations of this:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;from&lt;/SPAN&gt; datetime &lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; datetime

&lt;SPAN class="keyword token"&gt;def&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;getMonth&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;dateObj&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
  x &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; datetime&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;strftime&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;dateObj&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'%m'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
  &lt;SPAN class="keyword token"&gt;return&lt;/SPAN&gt; int&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;x&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;‍‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I get an error about&amp;nbsp;strftime needing a datetime object and not a unicode object.&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 00:42:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/datetime-to-string-or-datetime-to-integer/m-p/574065#M44975</guid>
      <dc:creator>MKF62</dc:creator>
      <dc:date>2021-12-12T00:42:41Z</dc:date>
    </item>
    <item>
      <title>Re: Datetime to String or Datetime to Integer</title>
      <link>https://community.esri.com/t5/python-questions/datetime-to-string-or-datetime-to-integer/m-p/574066#M44976</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;So while you can't cast the !Actual_Completion! field to a string directly using str() unless it's in an int() function for some reason I don't understand, I encoded the unicode value so it was a plain string and got it to work.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Codeblock:&amp;nbsp;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;def&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;getMonth&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;dateField&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
  dateString &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; dateField&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;encode&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'UTF8'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
  dateParts &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; dateString&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;split&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"/"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
  &lt;SPAN class="keyword token"&gt;return&lt;/SPAN&gt; int&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;dateParts&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;0&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;‍‍‍‍‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 00:42:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/datetime-to-string-or-datetime-to-integer/m-p/574066#M44976</guid>
      <dc:creator>MKF62</dc:creator>
      <dc:date>2021-12-12T00:42:44Z</dc:date>
    </item>
    <item>
      <title>Re: Datetime to String or Datetime to Integer</title>
      <link>https://community.esri.com/t5/python-questions/datetime-to-string-or-datetime-to-integer/m-p/574067#M44977</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Are you working with ArcMap or Pro?&amp;nbsp; The way each application handles ArcGIS Dates in Field Calculator differs.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 05 Sep 2019 16:38:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/datetime-to-string-or-datetime-to-integer/m-p/574067#M44977</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2019-09-05T16:38:48Z</dc:date>
    </item>
    <item>
      <title>Re: Datetime to String or Datetime to Integer</title>
      <link>https://community.esri.com/t5/python-questions/datetime-to-string-or-datetime-to-integer/m-p/574068#M44978</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;ArcMap 10.7&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 05 Sep 2019 17:30:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/datetime-to-string-or-datetime-to-integer/m-p/574068#M44978</guid>
      <dc:creator>MKF62</dc:creator>
      <dc:date>2019-09-05T17:30:48Z</dc:date>
    </item>
    <item>
      <title>Re: Datetime to String or Datetime to Integer</title>
      <link>https://community.esri.com/t5/python-questions/datetime-to-string-or-datetime-to-integer/m-p/574069#M44979</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The following should work for you:&lt;/P&gt;&lt;PRE class="language-python line-numbers"&gt;&lt;CODE&gt;int&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;!Actual_Completion!&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;split&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"/"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;0&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 05 Sep 2019 18:11:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/datetime-to-string-or-datetime-to-integer/m-p/574069#M44979</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2019-09-05T18:11:49Z</dc:date>
    </item>
    <item>
      <title>Re: Datetime to String or Datetime to Integer</title>
      <link>https://community.esri.com/t5/python-questions/datetime-to-string-or-datetime-to-integer/m-p/574070#M44980</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Interesting, that does work. Why is it thought that it can take unicode and go directly to integer? Guess I need to do more reading about unicode...&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 05 Sep 2019 18:18:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/datetime-to-string-or-datetime-to-integer/m-p/574070#M44980</guid>
      <dc:creator>MKF62</dc:creator>
      <dc:date>2019-09-05T18:18:17Z</dc:date>
    </item>
    <item>
      <title>Re: Datetime to String or Datetime to Integer</title>
      <link>https://community.esri.com/t5/python-questions/datetime-to-string-or-datetime-to-integer/m-p/574071#M44981</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Your issue wasn't really Unicode related.&amp;nbsp; Both Unicode and String are sequences, which means they can be indexed and sliced.&amp;nbsp; Looking at your first example:&lt;/P&gt;&lt;PRE class=""&gt;&lt;BLOCKQUOTE class="jive_macro_quote jive-quote jive_text_macro"&gt;&lt;CODE&gt;int&lt;SPAN class=""&gt;(&lt;/SPAN&gt;str&lt;SPAN class=""&gt;(&lt;/SPAN&gt;!Actual_Completion!&lt;SPAN class=""&gt;)&lt;/SPAN&gt;&lt;SPAN class=""&gt;[&lt;/SPAN&gt;&lt;SPAN class=""&gt;0&lt;/SPAN&gt;&lt;SPAN class=""&gt;]&lt;/SPAN&gt;&lt;SPAN class=""&gt;)&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/BLOCKQUOTE&gt;&lt;/PRE&gt;&lt;P&gt;!Actual_Completion! is returning a Unicode, str() is converting that to String, and then you are taking the first character of the string sequence.&amp;nbsp; What you needed to do is split the Unicode/string based on forward slashes and then retrieve the first item from the tuple returned from splitting the text up.&lt;/P&gt;&lt;PRE class=""&gt;&lt;CODE&gt;&lt;SPAN class=""&gt;&lt;/SPAN&gt;&lt;SPAN class=""&gt;&lt;/SPAN&gt;&lt;SPAN class=""&gt;&lt;/SPAN&gt;&lt;SPAN class=""&gt;&lt;/SPAN&gt;&lt;SPAN class=""&gt;&lt;/SPAN&gt;&lt;SPAN class=""&gt;&lt;/SPAN&gt;&lt;SPAN class=""&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 05 Sep 2019 18:27:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/datetime-to-string-or-datetime-to-integer/m-p/574071#M44981</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2019-09-05T18:27:11Z</dc:date>
    </item>
  </channel>
</rss>

