<?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 new field by adding 6 hours to date field error on midnight values in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/calculate-new-field-by-adding-6-hours-to-date/m-p/564367#M44173</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I found an example on the &lt;A href="https://desktop.arcgis.com/en/arcmap/10.3/manage-data/tables/calculate-field-examples.htm"&gt;Calculate Field examples&lt;/A&gt;&lt;/P&gt;&lt;PRE class="language-python line-numbers"&gt;&lt;CODE&gt;expression &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"arcpy.time.ParseDateTimeString(!DATE_UTC!) + timedelta(days=-0.25)"&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;This worked for me. Thanks!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 19 Dec 2019 16:32:07 GMT</pubDate>
    <dc:creator>AndrewL</dc:creator>
    <dc:date>2019-12-19T16:32:07Z</dc:date>
    <item>
      <title>Calculate new field by adding 6 hours to date field error on midnight values</title>
      <link>https://community.esri.com/t5/python-questions/calculate-new-field-by-adding-6-hours-to-date/m-p/564364#M44170</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello. I have the following code that works fine when there is not a record with a date/time of midnight. However today I came across a record at a time of midnight and it breaks the script. Is there a way around this? Thank you. I have provided a screenshot of the attributes. Both fields are date fields.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Error:&amp;nbsp;ValueError: time data '12/16/2019' does not match format '%m/%d/%Y %I:%M:%S %p'&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="comment token"&gt;# Add CST date/time field&lt;/SPAN&gt;
&lt;SPAN class="keyword token"&gt;print&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"Adding CST date field"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;AddField_management&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;mergedFCSquaresDis&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"DATE_CST"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"Date"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
expression &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"datetime.strptime(!DATE_UTC!,'%m/%d/%Y %I:%M:%S %p') + timedelta(hours=-6)"&lt;/SPAN&gt;
arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;CalculateField_management&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;mergedFCSquaresDis&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"DATE_CST"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; expression&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"PYTHON_9.3"&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;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 00:19:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-new-field-by-adding-6-hours-to-date/m-p/564364#M44170</guid>
      <dc:creator>AndrewL</dc:creator>
      <dc:date>2021-12-12T00:19:34Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate new field by adding 6 hours to date field error on midnight values</title>
      <link>https://community.esri.com/t5/python-questions/calculate-new-field-by-adding-6-hours-to-date/m-p/564365#M44171</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Andrew,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;lt;Edit: I just noticed that in your script you are subtracting 6 hours rather than adding as the heading states, the script below reflects the script as posted&amp;gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The issue seams to be that you are trying to force a format on to a field that does not have the information that you&amp;nbsp;are asking for.&lt;/P&gt;&lt;P&gt;If we remove the formatting from the equation, we can apply it after if needed, we can do the calculation on the base format.&lt;/P&gt;&lt;P&gt;I haven't had a chance to test this yet, but by the reading that i have done it should&amp;nbsp;work.&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="operator token"&gt;-&lt;/SPAN&gt;&lt;SPAN class="operator token"&gt;-&lt;/SPAN&gt;&lt;SPAN class="operator token"&gt;-&lt;/SPAN&gt;&lt;SPAN class="operator token"&gt;-&lt;/SPAN&gt;&lt;SPAN class="comment token"&gt;# Add CST date/time field&lt;/SPAN&gt;
&lt;SPAN class="keyword token"&gt;print&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"Adding CST date field"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;AddField_management&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;mergedFCSquaresDis&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"DATE_CST"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"Date"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
expression &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"!DATE_UTC! - 0.25"&lt;/SPAN&gt;
arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;CalculateField_management&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;mergedFCSquaresDis&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"DATE_CST"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; expression&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"PYTHON_9.3"&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;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Since the datetime fields store date values as the number of "Number of Days since 1/1/1900", see the tips at the bottom of the page of:&lt;BR /&gt;&lt;A class="link-titled" href="https://desktop.arcgis.com/en/arcmap/latest/manage-data/tables/working-with-date-fields-making-simple-date-field-.htm" title="https://desktop.arcgis.com/en/arcmap/latest/manage-data/tables/working-with-date-fields-making-simple-date-field-.htm" rel="nofollow noopener noreferrer" target="_blank"&gt;Making simple date field calculations using the field calculator—Help | ArcGIS Desktop&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;simply subtracting 6 hours (which works out at 0.25 of a day) from the DATE_UTC time should give you the result you are looking for.&lt;/P&gt;&lt;P&gt;Here&amp;nbsp;is a handy resource for dealing with Datetime in Python:&lt;BR /&gt;&lt;A class="link-titled" href="https://docs.python.org/2/library/datetime.html#timedelta-objects" title="https://docs.python.org/2/library/datetime.html#timedelta-objects" rel="nofollow noopener noreferrer" target="_blank"&gt;8.1. datetime — Basic date and time types — Python 2.7.17 documentation&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I hope that this helps&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Michael&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 00:19:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-new-field-by-adding-6-hours-to-date/m-p/564365#M44171</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-12-12T00:19:37Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate new field by adding 6 hours to date field error on midnight values</title>
      <link>https://community.esri.com/t5/python-questions/calculate-new-field-by-adding-6-hours-to-date/m-p/564366#M44172</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Michael. I tried that but I get error:&amp;nbsp;TypeError: unsupported operand type(s) for -: 'str' and 'float'&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I double checked the field type for DATE_UTC and it is a Date field. I am guessing that expression will not work with Python. But it looks like you are right in that we are trying to force a format on a field with a record that does not match that format.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I will do some more research. I think I may have to use an insert cursor method so that it can check each record before calculating.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 19 Dec 2019 14:58:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-new-field-by-adding-6-hours-to-date/m-p/564366#M44172</guid>
      <dc:creator>AndrewL</dc:creator>
      <dc:date>2019-12-19T14:58:02Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate new field by adding 6 hours to date field error on midnight values</title>
      <link>https://community.esri.com/t5/python-questions/calculate-new-field-by-adding-6-hours-to-date/m-p/564367#M44173</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I found an example on the &lt;A href="https://desktop.arcgis.com/en/arcmap/10.3/manage-data/tables/calculate-field-examples.htm"&gt;Calculate Field examples&lt;/A&gt;&lt;/P&gt;&lt;PRE class="language-python line-numbers"&gt;&lt;CODE&gt;expression &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"arcpy.time.ParseDateTimeString(!DATE_UTC!) + timedelta(days=-0.25)"&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;This worked for me. Thanks!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 19 Dec 2019 16:32:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-new-field-by-adding-6-hours-to-date/m-p/564367#M44173</guid>
      <dc:creator>AndrewL</dc:creator>
      <dc:date>2019-12-19T16:32:07Z</dc:date>
    </item>
  </channel>
</rss>

