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

Format date in R with lubridate

My input data, formatted as character, looks like this

"2020-07-10T00:00:00"

I tried

library(lubridate)
mdy_hms("2020-07-10T00:00:00", format='%Y-%m-%dT%H:%M:%S', tz=Sys.timezone())

But I get

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

[1] NA NA
Warning message:
All formats failed to parse. No formats found.

I tried the more flexibel approach parse_date_time(), but without luck

parse_date_time("2020-07-10T00:00:00", '%Y-%m-%dT%H:%M:%S', tz=Sys.timezone())

How can I convert this date "2020-07-10T00:00:00" to a date R recognizes? Note: I am not interested in the time really, only the date!

>Solution :

Assuming that the 07 is the month of July, and the 10 is the 10th:

x <- "2020-07-10T00:00:00"
ymd_hms(x, tz = Sys.timezone())

> [1] "2020-07-10 AEST"

If it’s in format year-day-month, swap the ymd for ydm.

Hope this helps!

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