<?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: calculate date field from string field in Geoprocessing Questions</title>
    <link>https://community.esri.com/t5/geoprocessing-questions/calculate-date-field-from-string-field/m-p/60809#M2152</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Hi I have the following python code where the field "datetime" is a string field and is different for each record. I want to convert the UTC time string to a date/time and then subtract 6 hours from it (CST).&lt;BR /&gt;&lt;BR /&gt;It is telling me that "!datetime!" does not match the format of "%Y%m%d%H%M". The actual records within the field do match that format, but I think it is trying to use the string "!datetime!" rather than using the data within that field name.&lt;BR /&gt;&lt;BR /&gt;I wonder if this is a quick formatting fix or I need to set up a cursor to go through each field? Thank you.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;arcpy.CalculateField_management(pt, "DATE_CST", dt.strptime("!datetime!", "%Y%m%d%H%M")&amp;nbsp; - timedelta(hours=6), "PYTHON_9.3")&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Build it in Modelbuilder and export a working calculation to a Python script to see the correct way to quote your expression.&amp;nbsp; The whole expression needs to be quoted.&amp;nbsp; No calculation or conversion of any dates should be done by your python script.&amp;nbsp; The only date conversion is done directly by the Calculate Field tool after it passes a string expression to its own internal Python interpreter which does the actual parsing of the expression that leads to the conversion.&amp;nbsp; You are trying to make Python interpret the expression before passing it to the tool.&amp;nbsp; As far as your Python script is concerned it should just see this entire expression as a string and nothing more.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 26 Feb 2014 19:30:18 GMT</pubDate>
    <dc:creator>RichardFairhurst</dc:creator>
    <dc:date>2014-02-26T19:30:18Z</dc:date>
    <item>
      <title>calculate date field from string field</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/calculate-date-field-from-string-field/m-p/60808#M2151</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi I have the following python code where the field "datetime" is a string field and is different for each record. I want to convert the UTC time string to a date/time and then subtract 6 hours from it (CST).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It is telling me that "!datetime!" does not match the format of "%Y%m%d%H%M". The actual records within the field do match that format, but I think it is trying to use the string "!datetime!" rather than using the data within that field name.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I wonder if this is a quick formatting fix or I need to set up a cursor to go through each field? Thank you.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;arcpy.CalculateField_management(pt, "DATE_CST", dt.strptime("!datetime!", "%Y%m%d%H%M")&amp;nbsp; - timedelta(hours=6), "PYTHON_9.3")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Feb 2014 18:37:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/calculate-date-field-from-string-field/m-p/60808#M2151</guid>
      <dc:creator>AndrewL</dc:creator>
      <dc:date>2014-02-26T18:37:50Z</dc:date>
    </item>
    <item>
      <title>Re: calculate date field from string field</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/calculate-date-field-from-string-field/m-p/60809#M2152</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Hi I have the following python code where the field "datetime" is a string field and is different for each record. I want to convert the UTC time string to a date/time and then subtract 6 hours from it (CST).&lt;BR /&gt;&lt;BR /&gt;It is telling me that "!datetime!" does not match the format of "%Y%m%d%H%M". The actual records within the field do match that format, but I think it is trying to use the string "!datetime!" rather than using the data within that field name.&lt;BR /&gt;&lt;BR /&gt;I wonder if this is a quick formatting fix or I need to set up a cursor to go through each field? Thank you.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;arcpy.CalculateField_management(pt, "DATE_CST", dt.strptime("!datetime!", "%Y%m%d%H%M")&amp;nbsp; - timedelta(hours=6), "PYTHON_9.3")&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Build it in Modelbuilder and export a working calculation to a Python script to see the correct way to quote your expression.&amp;nbsp; The whole expression needs to be quoted.&amp;nbsp; No calculation or conversion of any dates should be done by your python script.&amp;nbsp; The only date conversion is done directly by the Calculate Field tool after it passes a string expression to its own internal Python interpreter which does the actual parsing of the expression that leads to the conversion.&amp;nbsp; You are trying to make Python interpret the expression before passing it to the tool.&amp;nbsp; As far as your Python script is concerned it should just see this entire expression as a string and nothing more.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Feb 2014 19:30:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/calculate-date-field-from-string-field/m-p/60809#M2152</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2014-02-26T19:30:18Z</dc:date>
    </item>
    <item>
      <title>Re: calculate date field from string field</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/calculate-date-field-from-string-field/m-p/60810#M2153</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thank you. I found some info on the ArcGIS help pages. Here is what I did.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;arcpy.ConvertTimeField_management(pt,"datetime","yyyyMMddHHmmss, yyyy/MM/dd HH:mm:ss", "DATETIME_Converted") arcpy.AddField_management(pt, "DATETIME_CST", "DATE") arcpy.CalculateField_management(pt, "DATETIME_CST", "DateAdd(\"h\",-6,[DATETIME_Converted])", "VB", "")&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 27 Feb 2014 19:01:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/calculate-date-field-from-string-field/m-p/60810#M2153</guid>
      <dc:creator>AndrewL</dc:creator>
      <dc:date>2014-02-27T19:01:43Z</dc:date>
    </item>
  </channel>
</rss>

