I need to parse times in a character format like "1:36:22 PM".
I've tried various permutations of as.POSIXct
and strptime
but just can't get there. For example, this fails to pick up the importance of PM:
t <- "1:36:22 PM"
as.POSIXct(t, format = "%H:%M:%S")
# [1] "2016-09-08 01:36:22 BST"
and this fails:
strptime(t, "%H:%M:%S")
# [1] NA
Curiously, for reasons I can't understand, dropping the seconds from the input may work:
t <- "1:30:00 PM"
strptime(t, "%I:%M %p")
# [1] NA
t <- "1:30 PM"
strptime(t, "%I:%M %p")
# [1] "2016-09-08 13:30:00 BST"
All I want is for this to work:
t <- "1:30:00 PM"
SOME COMMAND HERE HERE
# [1] "13:30:00"
Any ideas?