Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

datetime

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"
like image 866
Nishant Srivastava Avatar asked Sep 14 '25 17:09

Nishant Srivastava


1 Answers

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
like image 96
akrun Avatar answered Sep 16 '25 06:09

akrun