How to convert AM/PM format into hms (and other ideas)?

53 views Asked by At

I have a dataset that looks like this

Data Representative(Header)

I am trying to combine shift_date with shift_start and shift_end in the same way arrive and leave are done, so that I can compare these two data points.

I have tried lubridate and strptime but maybe the "a.m." instead of "AM" is throwing it off. I also tried other ways of formatting it using %I %p but that also did not work. Any suggestions on how I should try to solve this?

1

There are 1 answers

0
Roland On
x <- "17-May-82"
y <- "1 p.m."

#if locale isn't an English one:
lct <- Sys.getlocale("LC_TIME"); Sys.setlocale("LC_TIME", "C")

#adjust timezone as needed
#use gsub to remove dots
as.POSIXct(paste(x, gsub(".", "", y, fixed = TRUE)), "%d-%b-%y %I %p", tz = "GMT")
#[1] "1982-05-17 13:00:00 GMT"

#reset locale
Sys.setlocale("LC_TIME", lct)