# Calculating total time, last value minus first value in ArcPy?

For the script tool I've written (below), one of the things I want to do is after user input has pulled tabular data (into a feature class) I want to take the last value of the 'logdt' and subtract it by the first value of the 'logdt'. 'logdt' being a datetime value in this format '2/1/2014 10:00:11 AM'. The reason being is that I want to determine the total length of time that has passed since the first and last pings for a specific vehicle in a given day. Once I have made this minor calculation I will then use it to determine how many, if any, lost pings occurred that day.

I'm sure it's very simple but I am newer to arcpy/python and cannot think of a way to do the logdt.lastValue - logdt.firstValue subtraction.

*EDIT: Previous code has been re-written and only pertinent code below is shown.

``````# Calculates the total number of rows for newly added column "Total_Pings"
numRows = int(arcpy.GetCount_management(queryLayerName).getOutput(0))

# UpdateCursor that will write the value of numRows to the "Total_Pings" column
cursor = arcpy.da.UpdateCursor(queryLayerName, "Total_Pings")
for row in cursor:
row[0] = numRows
cursor.updateRow(row)

# SearchCursor that will read the values of "LOGDT" and return the first value (earliest time)
firstLogdt = [row[0] for row in arcpy.da.SearchCursor(queryLayerName, "LOGDT")][0]

# UpdateCursor that will write the first (earliest time) "LOGDT" value to the field "First_Time"
cursor = arcpy.da.UpdateCursor(queryLayerName, "First_Time")
for row in cursor:
row[0] = firstLogdt
cursor.updateRow(row)

# SearchCursor that will read the values of "LOGDT" and return the last value (latest time)
lastLogdt = [row[0] for row in arcpy.da.SearchCursor(queryLayerName, "LOGDT")][-1]

# UpdateCursor that will write the last (latest time) "LOGDT" value to the field "Last_Time"
cursor = arcpy.da.UpdateCursor(queryLayerName, "Last_Time")
for row in cursor:
row[0] = lastLogdt
cursor.updateRow(row)
``````

I am receiving the following error in Python:

• That error suggests that your data is already in a datetime format, so you don't need to use datetime.datetime.strptime to convert it. Try doing that again, but print type(firstLogdt). If it is <type 'datetime.datetime'> then just do lastLogdt - firstLogdt.
– spg
Commented Jun 16, 2015 at 1:17
• ^ Hah, yeah very rookie mistake on my part. I was assuming (after making other changes) that I still had to worry about the datetime format. Now I'm just going to work out how to display this in solely hours. Commented Jun 16, 2015 at 12:55
• Please always include errors as text rather than pictures so that they are available to future searches.
– PolyGeo
Commented Sep 9, 2016 at 19:23

Assuming that you want to get the "logdt" values from the queryLayerName, you can set up a search cursor

``````#number of records might be handy for your calculation of number of pings missed?
numRows = int(arcpy.GetCount_management(queryLayerName).getOutput(0))

logdt_first = 0 # or datatype as appropriate for your field
logdt_last = 0
with arcpy.da.SearchCursor(queryLayerName, 'logdt') as cursor: # 'logdt' is the name of the field
for row in cursor:
print row[0]
if logdt_first == 0:
logdt_first = row[0]
if row[0] < logdt_first:
logdt_firs = row[0]
if row[0] > logdt_last:
logdt_last = row[0]

logdt_diff = logdt_last - logdt_first
``````

You should be able to run the above in the python window in ArcCatalog to quickly test the datatype needed for the field to make sure that you can calculate it, you may need to convert a datestring to a date for example before comparison (see Cody's answer above for details on how that could work for you).

• Thanks for the help, unfortunately I was unable to get it to work. However, this is on my end, as I'm having trouble with writing the script. Will gather my thoughts and have to come back to this later. Commented Jun 11, 2015 at 19:58
• What sort of trouble? I have edited the script slightly which might clarify, you should be able to run this in the intermediate window to test. bear in mind that you will have to confirm what format your logdt field is in, I have given an example with a simple numeric example.
– spg
Commented Jun 12, 2015 at 22:09
• ^ My 'LOGDT' is a DATE field and I changed its format - I've edited and updated my original post. Thanks again for your help! Commented Jun 15, 2015 at 22:20

You could use the datetime library with python to figure the time out.

If your format is always like "2/1/2014 10:00:11 AM" (i'm assuming Day/Month/Year) then its pretty straight forward.

``````import datetime

dtFormat = "%d/%m/%Y %I:%M:%S %p"

logdt_FirstRaw = '2/1/2014 10:00:11 AM'
logdt_FirstDT = datetime.datetime.strptime(logdt_FirstRaw , dtFormat)

logdt_LastRaw = '2/1/2014 11:00:00 AM'
logdt_LastDT = datetime.datetime.strptime(logdt_LastRaw , dtFormat)

logdt_Difference = logdt_LastDT - logdt_FirstDT
#prints '0:59:49'
``````

if the format is month/day/year, then flip the %d and %m

hope this helps!

• Thanks for your help Cody! I've been able to achieve pulling the first and last values from the user's pulled data (as edited in my post above), but cannot figure out how to get the datetime subtraction to work. Commented Jun 15, 2015 at 22:26