<?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 String to Datetime with Python in ArcGIS Pro in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/string-to-datetime-with-python-in-arcgis-pro/m-p/1342013#M69045</link>
    <description>&lt;P&gt;I want to create a datetime column in a feature class from a string field. The string field has this format: "%Y-%m-%dT%H:%M:%S" (for example: "2023-10-16T07:50:00"). When I use the gp tool "ConvertTimeField", some (about 10%) columns contain milliseconds, but the string does not contain milliseconds.&lt;/P&gt;&lt;P&gt;For example: string: 2023-10-16T07:50:25; output time: 10/16/2023 07:50:24; datetime object: datetime.datetime(2023, 10, 16, 7, 50, 24, 999000)&lt;/P&gt;&lt;P&gt;This also occurs when converting the time with "datetime.datetime.strptime()" when I run the script in the ArcGIS Pro Python window. If I use the same code from the script in ArcGIS Notebooks and Visual Studio Code, it works correctly.&lt;/P&gt;&lt;P&gt;Does anyone know this problem and can help me?&lt;BR /&gt;Thomas&lt;/P&gt;</description>
    <pubDate>Thu, 26 Oct 2023 15:08:42 GMT</pubDate>
    <dc:creator>Thomas_Knaeuper</dc:creator>
    <dc:date>2023-10-26T15:08:42Z</dc:date>
    <item>
      <title>String to Datetime with Python in ArcGIS Pro</title>
      <link>https://community.esri.com/t5/python-questions/string-to-datetime-with-python-in-arcgis-pro/m-p/1342013#M69045</link>
      <description>&lt;P&gt;I want to create a datetime column in a feature class from a string field. The string field has this format: "%Y-%m-%dT%H:%M:%S" (for example: "2023-10-16T07:50:00"). When I use the gp tool "ConvertTimeField", some (about 10%) columns contain milliseconds, but the string does not contain milliseconds.&lt;/P&gt;&lt;P&gt;For example: string: 2023-10-16T07:50:25; output time: 10/16/2023 07:50:24; datetime object: datetime.datetime(2023, 10, 16, 7, 50, 24, 999000)&lt;/P&gt;&lt;P&gt;This also occurs when converting the time with "datetime.datetime.strptime()" when I run the script in the ArcGIS Pro Python window. If I use the same code from the script in ArcGIS Notebooks and Visual Studio Code, it works correctly.&lt;/P&gt;&lt;P&gt;Does anyone know this problem and can help me?&lt;BR /&gt;Thomas&lt;/P&gt;</description>
      <pubDate>Thu, 26 Oct 2023 15:08:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/string-to-datetime-with-python-in-arcgis-pro/m-p/1342013#M69045</guid>
      <dc:creator>Thomas_Knaeuper</dc:creator>
      <dc:date>2023-10-26T15:08:42Z</dc:date>
    </item>
    <item>
      <title>Re: String to Datetime with Python in ArcGIS Pro</title>
      <link>https://community.esri.com/t5/python-questions/string-to-datetime-with-python-in-arcgis-pro/m-p/1342420#M69051</link>
      <description>&lt;P&gt;I'm not sure where this ms level noise is coming from (especially with strptime) but &lt;A href="https://stackoverflow.com/a/49309848" target="_self"&gt;this StackOverflow answer&lt;/A&gt; looks like a valid solution when combined with a good old Field Calculation run.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Oct 2023 23:35:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/string-to-datetime-with-python-in-arcgis-pro/m-p/1342420#M69051</guid>
      <dc:creator>DavidSolari</dc:creator>
      <dc:date>2023-10-26T23:35:59Z</dc:date>
    </item>
    <item>
      <title>Re: String to Datetime with Python in ArcGIS Pro</title>
      <link>https://community.esri.com/t5/python-questions/string-to-datetime-with-python-in-arcgis-pro/m-p/1342841#M69062</link>
      <description>&lt;P&gt;I've seen this behavior in Python often enough. I just assumed it was quirk of how Python operates -- if you don't fill all the values, it uses the value with which the object was initialized (the current time). So I usually combine &lt;FONT face="courier new,courier"&gt;strptime&lt;/FONT&gt; with a &lt;FONT face="courier new,courier"&gt;replace&lt;/FONT&gt;: to clear the microseconds property (and to either clear or force a UTC timezone)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import datetime

test_str = '2023-10-16T07:50:00'
new_dt = datetime.datetime.strptime(
                test_str,'%Y-%m-%dT%H:%M:%S').replace(
                microsecond=0,tzinfo=None)
print(new_dt.strftime('%Y-%m-%d %H:%M:%S.%f %Z'))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The funny thing is, I can't reproduce the problem in the Python in my Pro 3.1.1 or ArcMap 10.8.2 installs, so maybe that Python "feature" has been cleared.&lt;/P&gt;&lt;P&gt;What version of ArcGIS are you using for the&amp;nbsp;&lt;SPAN&gt;ConvertTimeField utility?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;- V&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Oct 2023 17:35:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/string-to-datetime-with-python-in-arcgis-pro/m-p/1342841#M69062</guid>
      <dc:creator>VinceAngelo</dc:creator>
      <dc:date>2023-10-27T17:35:39Z</dc:date>
    </item>
    <item>
      <title>Re: String to Datetime with Python in ArcGIS Pro</title>
      <link>https://community.esri.com/t5/python-questions/string-to-datetime-with-python-in-arcgis-pro/m-p/1343115#M69065</link>
      <description>&lt;P&gt;Thank you very much for your help!&lt;BR /&gt;My ArcGIS Pro is running with version 3.1.3.&lt;/P&gt;&lt;P&gt;I have solved the problem myself. After changing the values in the attribute table, I used "arcpy.management.CopyFeatures" to copy the memory feature class to disk. If I don't run this tool or run it before, the datetime conversion works correctly.&lt;/P&gt;</description>
      <pubDate>Sun, 29 Oct 2023 13:37:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/string-to-datetime-with-python-in-arcgis-pro/m-p/1343115#M69065</guid>
      <dc:creator>Thomas_Knaeuper</dc:creator>
      <dc:date>2023-10-29T13:37:26Z</dc:date>
    </item>
  </channel>
</rss>

