Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to turn PM/AM time stamp am/pm

I have a dataset with a column where date and time is stored.
The data I have is:
03/17/2020 09:30:00 PM

I want to convert AM/PM to a 24hour format.
My attempt was using this:

as.POSIXct(df$Date, format="%d/%m/%Y %I:%M:%S %p", tz="UTC")

When I run this with the whole dataset, the majority of dates turns into "NA".
Why is this happening? I am really confused.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Using lubridate:

x <- "03/17/2020 09:30:00 PM"

lubridate::mdy_hms(x)
[1] "2020-03-17 21:30:00 UTC"

Using as.Posixct: note that you need the month / day convention, not the day/month:

as.POSIXct(x, format="%m/%d/%Y %I:%M:%S %p", tz = "UTC")
[1] "2020-03-17 21:30:00 UTC"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading