How to convert timestamp with AM / PM at the end of a line?

I am trying to convert this timestamp to POSIXct

t1 <- c("19-Jun-13 06.00.00.00 PM") 

If I do this:

 t1 <- as.POSIXct(t1, format="%d-%b-%y %H:%M:%S") 

can this convert this timestamp? Does this end with an AM / PM statement?

+6
source share
2 answers

Read ?strptime . %p , which only works with %I , not %H Your time format is also incorrect. Your time is divided into "." , not ":" .

 as.POSIXct("19-Jun-13 06.00.00.00 PM", format="%d-%b-%y %I.%M.%OS %p") 
+7
source

I donโ€™t understand why strptime does not recognize the %p format. However, the dmy_hms function from the lubridate package works well.

lubridate::dmy_hms("19-Jun-13 06.00.00.00 PM") produces the following result:

[1] "2013-06-19 18:00:00 UTC"

which you can "reformat" if you want, say Ymd H: M:

as.POSIXct(dmy_hms("19-Jun-13 06.00.00.00 PM"), format="%Y-%m-%d %H:%M") [1] "2013-06-19 18:00:00 UTC"

0
source

Source: https://habr.com/ru/post/947849/


All Articles