Nishant Srivastava
Nishant Srivastava

Reputation: 449

How to convert "HH:MM:SS AM/PM" to 24 hour format in R?

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

Answers (1)

akrun
akrun

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

Related Questions