I have two text fields (STRTTIME, ENDTIME) which are are formatted as military time with leading zeros. For example, 0540, 1655.
I want to calculate a new field with the time duration as integers (in minutes). How can I go about this using the Field Calculator in ArcMap?
Dan,
You can use a Python function in field calculator to determine difference in time. Since your data is in military time with leading zeros it is even easier. Create a function as noted below. This function takes the string parameters, convers them to datatime variables, calculates the difference in seconds and return this value multiplied by 60 to give you minutes. This script does assume the times are within a single 24-hour day and that STRTTIME is before ENDTIME. It can be modified to include times on multiple days if the date is provided as well.
<SPAN class="keyword token">import</SPAN> datetime <SPAN class="keyword token">def</SPAN> deltaTime <SPAN class="punctuation token">(</SPAN>start<SPAN class="punctuation token">,</SPAN> end<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> starttime <SPAN class="operator token">=</SPAN> datetime<SPAN class="punctuation token">.</SPAN>datetime<SPAN class="punctuation token">.</SPAN>strptime<SPAN class="punctuation token">(</SPAN>start<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'%H%M'</SPAN><SPAN class="punctuation token">)</SPAN> endtime <SPAN class="operator token">=</SPAN> datetime<SPAN class="punctuation token">.</SPAN>datetime<SPAN class="punctuation token">.</SPAN>strptime<SPAN class="punctuation token">(</SPAN>end<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'%H%M'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">return</SPAN> <SPAN class="punctuation token">(</SPAN>endtime <SPAN class="operator token">-</SPAN> starttime<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>seconds<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="operator token">/</SPAN><SPAN class="number token">60</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
You can call this function in field calculator using deltaTime(!STRTTIME!, !ENDTIME!)
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.