<?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 Date and time calculations in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/date-and-time-calculations/m-p/234197#M18160</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Just wondering if anyone can tell me why my expression is not working to calculate some values in a table?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Below the !DateTime! and !Time! are fields in a File geodatabase. Basically I need to combine the two fields, where DateTime currently has the correct date for the record and !Time! has a random date and the correct time I need combined with the first field.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt; datetime.combine (datetime.strptime ( !DateTime!, %Y %m %d), datetime.strptime (!Time!, %H:%M:%S)) &lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 23 Jan 2014 05:25:38 GMT</pubDate>
    <dc:creator>TimothyElliott</dc:creator>
    <dc:date>2014-01-23T05:25:38Z</dc:date>
    <item>
      <title>Date and time calculations</title>
      <link>https://community.esri.com/t5/python-questions/date-and-time-calculations/m-p/234197#M18160</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Just wondering if anyone can tell me why my expression is not working to calculate some values in a table?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Below the !DateTime! and !Time! are fields in a File geodatabase. Basically I need to combine the two fields, where DateTime currently has the correct date for the record and !Time! has a random date and the correct time I need combined with the first field.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt; datetime.combine (datetime.strptime ( !DateTime!, %Y %m %d), datetime.strptime (!Time!, %H:%M:%S)) &lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 23 Jan 2014 05:25:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/date-and-time-calculations/m-p/234197#M18160</guid>
      <dc:creator>TimothyElliott</dc:creator>
      <dc:date>2014-01-23T05:25:38Z</dc:date>
    </item>
    <item>
      <title>Re: Date and time calculations</title>
      <link>https://community.esri.com/t5/python-questions/date-and-time-calculations/m-p/234198#M18161</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Well, the strptime patterns are strings. They need to be quoted.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;as in: "%Y %m %d" and "%H:%M:%S"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Then, argument 2 for combine() must be datetime.time, not datetime.datetime...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;... are the !DateTime! and !Time! fields strings, or are they actual date fields in the table?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If the dates are date objects with 00:00:00 time components, &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;You might be able to add the time field value to the dateTime field value&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 24 Jan 2014 14:11:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/date-and-time-calculations/m-p/234198#M18161</guid>
      <dc:creator>markdenil</dc:creator>
      <dc:date>2014-01-24T14:11:59Z</dc:date>
    </item>
    <item>
      <title>Re: Date and time calculations</title>
      <link>https://community.esri.com/t5/python-questions/date-and-time-calculations/m-p/234199#M18162</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;&lt;BR /&gt;... are the !DateTime! and !Time! fields strings, or are they actual date fields in the table?&lt;BR /&gt;&lt;BR /&gt;If the dates are date objects with 00:00:00 time components, &lt;BR /&gt;You might be able to add the time field value to the dateTime field value&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The fields are both dates DateTime has values: 20/01/2014 and Time has values: 23/01/2014 8:52:03 AM&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The problem is that the Time field should have no date in it but part of the export process it gets a date added and is incorrectly given the date the file was processed.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Essentially what I want is a field with 20/01/2014 8:52:03 AM, generated from the two fields. I tried the addition but I can't get it to ignore the date in the Time field.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 26 Jan 2014 04:08:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/date-and-time-calculations/m-p/234199#M18162</guid>
      <dc:creator>TimothyElliott</dc:creator>
      <dc:date>2014-01-26T04:08:30Z</dc:date>
    </item>
    <item>
      <title>Re: Date and time calculations</title>
      <link>https://community.esri.com/t5/python-questions/date-and-time-calculations/m-p/234200#M18163</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;take each time object and deconstruct it to its component parts with datetime.timetuple()&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; import datetime
&amp;gt;&amp;gt;&amp;gt; d = datetime.datetime.now()
&amp;gt;&amp;gt;&amp;gt; d
datetime.datetime(2014, 1, 28, 15, 28, 21, 470000)
&amp;gt;&amp;gt;&amp;gt; tup = d.timetuple()
&amp;gt;&amp;gt;&amp;gt; tup
time.struct_time(tm_year=2014, tm_mon=1, tm_mday=28, tm_hour=15, tm_min=28, tm_sec=21, tm_wday=1, tm_yday=28, tm_isdst=-1)
&amp;gt;&amp;gt;&amp;gt; for t in tup:
 print t
2014
1
28
15
28
21
1
28
-1
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;assign the date parts from the !Date! field to variables and the time parts from the !Time! field&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;then assemble a new new date-time object to insert in the desired field&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;using strptime()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;or use datetime.replace to replace the time parts of the original date&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 11:50:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/date-and-time-calculations/m-p/234200#M18163</guid>
      <dc:creator>markdenil</dc:creator>
      <dc:date>2021-12-11T11:50:22Z</dc:date>
    </item>
    <item>
      <title>Re: Date and time calculations</title>
      <link>https://community.esri.com/t5/python-questions/date-and-time-calculations/m-p/234201#M18164</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;This may illustrate:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;###&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dateAndHour.py
import datetime

hrObj = datetime.datetime(1999, 02, 06, 05, 40, 29)
dtObj = datetime.datetime(2014, 1, 29, 00, 00, 00)

t = hrObj.timetuple()
d = dtObj.timetuple()

newDate = datetime.datetime(
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; d[0],
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; d[1],
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; d[2],
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; t[3],
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; t[4],
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; t[5])
print hrObj
print dtObj
print newDate&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;input your own date and time field values.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 11:50:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/date-and-time-calculations/m-p/234201#M18164</guid>
      <dc:creator>markdenil</dc:creator>
      <dc:date>2021-12-11T11:50:25Z</dc:date>
    </item>
    <item>
      <title>Re: Date and time calculations</title>
      <link>https://community.esri.com/t5/python-questions/date-and-time-calculations/m-p/234202#M18165</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;after lots of hunting and a ESRI support ticket later I have found out that&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;&lt;BR /&gt;the Field Caclulator and by extension, the Calculate Field geoprocessing tool access the date and time values as strings. In a file geodatabase these are all unicode strings, though you may find they're not in the case of a personal geodatabase.&amp;nbsp; &lt;BR /&gt;&lt;BR /&gt;We can use datetime.strptime to convert the two strings of text into datetime objects. Then we can access the properties of each of these objects to pull out information about the day / month / year in the case of our field with only the date, in it, or hour / minutes / seconds in the case of our field containing both a date and time. When the Calculate Field goes to fill in the time information in the combined field, it wants this data to be provided as a datetime object though. So this makes our best bet, to take the information we're interested from each of our two existing datetime objects, and create a new datetime object using datetime.datetime(yeah, month, day, hour, minute, second).&lt;BR /&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;Expression: combine_datefields(!Time_!, !DateTime_!) Codeblock: def combine_datefields(time_value, date_value): &amp;nbsp;&amp;nbsp;&amp;nbsp; t = datetime.datetime.strptime( time_value.encode('utf-8'), "%d/%m/%Y %I:%M:%S %p")&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; d =&amp;nbsp; datetime.datetime.strptime( date_value.encode('utf-8'), "%d/%m/%Y") &amp;nbsp;&amp;nbsp;&amp;nbsp; combined_dt = datetime.datetime(d.year, d.month, d.day, t.hour, t.minute, t.second) &amp;nbsp;&amp;nbsp;&amp;nbsp; return combined_dt&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So as a result the Datetime.combine was not the right tool for the job&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 01 Feb 2014 00:29:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/date-and-time-calculations/m-p/234202#M18165</guid>
      <dc:creator>TimothyElliott</dc:creator>
      <dc:date>2014-02-01T00:29:12Z</dc:date>
    </item>
  </channel>
</rss>

