0
$\begingroup$

I have the following date format

2018-02-06T16:50:36Z

I am trying to change it to only date format in R using the following however it is not working:

date_1 <- data %>% mutate(date_time_submitted = as.Date(date_time_submitted, format = "%Y-%m-%d:%H:%M:%S:%Z"))

Any ideas?

Thanks

$\endgroup$

1 Answer 1

0
$\begingroup$

You are just using the incorrect format as you are missing the T character and have an extra : character between the day and hour and the seconds and timezone indicator. Changing the format to "%Y-%m-%dT%H:%M:%SZ" gives the correct date:

> as.Date("2018-02-06T16:50:36Z", format="%Y-%m-%dT%H:%M:%SZ")
[1] "2018-02-06"

Another option would be to use the as_date/as_datetime function from the lubridate package, which give the correct date(time) without specifying the format:

> lubridate::as_date("2018-02-06T16:50:36Z")
[1] "2018-02-06"
> lubridate::as_datetime("2018-02-06T16:50:36Z")
[1] "2018-02-06 16:50:36 UTC"
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.