Convert 12 hour to 24 hour format in R
<p>I am not getting the right conversion when I try to convert 12 hours to 24 hours. My script (with sample data is below). </p>

<pre><code>library(lubridate)
library(data.table)

# Read Sample Data
df &lt;- data.frame(c("April 22 2016 10:49:15 AM","April 22 2016 10:01:21 AM","April 22 2016 09:06:40 AM","April 21 2016 09:50:49 PM","April 21 2016 06:07:18 PM"))
colnames(df) &lt;- c("Date") # Set Column name
dt &lt;- setDT(df) # Convert to data.table

ff &lt;- function(x) as.POSIXlt(strptime(x,"%B %d %Y %H:%M:%S %p"))
dt[,dates := as.Date(ff(Date))]
</code></pre>

<p>When I try creating a new variable called TOD, I get the output in H:M:S format without converting it into 24 hour format. <strong>What I mean is that for the 3rd row, instead of getting 21:50:49 I get 09:50:49.</strong> I tried two different ways to do this. One use as.ITime from data.table and then also using strptime. The code I use to calculate TOD is below.</p>

<pre><code>dt[,TOD1 := as.ITime(ff(Date))]
dt$TOD2 &lt;- format(strptime(dt$Date, "%B %d %Y %H:%M:%S %p"), format="%I:%M:%S")
</code></pre>

<p>I thought of trying it using dataframe instead of data.table to eliminate any issues with using strptime in data.table and still got the same answer. </p>

<pre><code>df$TOD &lt;- format(strptime(df$Date, "%B %d %Y %H:%M:%S %p"), format="%I:%M:%S") # Using dataframe instead of data.table
</code></pre>

<p>Any insights on how to get the right answer? </p>