Reputation: 449
Is there a way to convert an string in format "HH:MM:SS AM/PM" to 24 hour format in R.
I tried HMS but it didnot work.
When i am using strptime it is giving me date as well which i dont want.
strptime(c("1:00:29 AM","1:00:29 PM"), "%I:%M:%S %p")
#output "2016-08-07 01:00:29 IST" "2016-08-07 13:00:29 IST"
Upvotes: 4
Views: 10977
Reputation: 887851
May be we can use format
time <- format(strptime(c("1:00:29 AM","1:00:29 PM"), "%I:%M:%S %p"), "%H:%M:%S")
time
#[1] "01:00:29" "13:00:29"
This can be converted to times
class using ?times
from chron
library(chron)
times(time)
#[1] 01:00:29 13:00:29
Upvotes: 8