title: How to get seconds elapsed from a list of time strings in R?
body: <p>I am new to playing with time on R and am stuck on a task. I have a list of time strings of the form &quot;11:00:12 AM&quot; i.e. &quot;hour:minutes:seconds AM/PM&quot; in chronological order. For each entry, I would like to obtain the seconds elapsed from the first time. For example, if the entries are &quot;11:00:12 PM&quot;, &quot;11:00:32 PM&quot;, &quot;11:00:42 PM&quot; I would like a list of the form  &quot;0, 20, 30 ...&quot;</p>
  <p>Note that the strings do not contain date stamps - just time.</p>
  <p>I tried using <code>parse_date_time</code> and <code>sapply</code> and got named numeric vectors which don't seem to recognize the time ordering. To illustrate, here's a sample code:</p>
  <pre><code>Time_list=c(&quot;11:00:12 PM&quot;, &quot;11:00:32 PM&quot;, &quot;11:00:42 PM&quot;, &quot;12:00:12 AM&quot;, &quot;12:00:32 AM&quot;, &quot;12:00:42 AM&quot;)
  new_list= sapply(Time_list, FUN=parse_date_time, orders='%I:%M:%S %p')
  </code></pre>
  <p>The obtained output is:</p>
  <pre><code>&gt; new_list  
   11:00:12 PM  11:00:32 PM  11:00:42 PM  12:00:12 AM  12:00:32 AM  12:00:42 AM
  -62167136388 -62167136368 -62167136358 -62167219188 -62167219168 -62167219158
  </code></pre>
  <p>As noticeable, it treats 12:00:12AM as a time <strong>prior</strong> to 11:00:42PM and I get the following output for time differences</p>
  <pre><code>&gt; new_list[3]-new_list[4]
  11:00:42 PM 
        82830 
  
  &gt; new_list[4]-new_list[3]
  12:00:12 AM 
       -82830 
  
  &gt; difftime(new_list[3], new_list[4])
  Error in as.POSIXct.numeric(time1) : 'origin' must be supplied
  </code></pre>
  <p>and I am unsure of how to fix this. I realize I could manually add a date and change it to the next day at 12:00 AM, however, I have 100 such files that start at different times so this would be unfeasible, and I would prefer to automate this via code.</p>
  <p>Any input is much appreciated!</p>
  <p>Thanks.</p>