Select to view content in your preferred language

Time Difference to decimal

164
1
10-09-2024 01:52 PM
ModernElectric
Frequent Contributor

I am working with a File GDB Feature Class thru Field Calculate (Python or Arcade) and trying to figure out HOW to calculate the difference between the Start Time and End Time.

Example: 

   Start Time: 7:00 AM

   End Time: 2:15 PM

   Time Difference: 2.25

How do I go about doing this? My other research results in just a flat hour or minutes and cannot figure out how to convert to the actual decimal difference.

Thank You

0 Kudos
1 Reply
DavidSolari
Frequent Contributor

If you have proper date fields in your data, they'll be converted into Python datetime objects when you run Calculate Field in Python3 mode. This means you can put something like this in your code block to get a decimal result:

 

def run(start, end):
    diff = end - start  # creates datetime.TimeDelta object
    return diff.hours + (diff.minutes / 60.0)

 

Exercise for the reader: handle cases with null inputs.

0 Kudos