Home > other >  Format date in R with lubridate
Format date in R with lubridate

Time:06-27

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

[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!

CodePudding user response:

Why not just

as.Date("2020-07-10T00:00:00")
# [1] "2020-07-10"

Fun fact:

as.Date("2020-07-101sddT00:1sdafsdfsdf0:00sdfzsdfsdfsdf")
# [1] "2020-07-10"

CodePudding user response:

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!

  • Related