Help with format date

Hi,

I have problems formating the following date in rstudio:

17/04/20 17:48:04,000000000
17/04/20 21:47:12,000000000
15/04/20 22:19:43,000000000

I need some help to format them in the following format:

2020-04-10 11:11:29

Any help will be really apreciat it.

Thanks in advance!

Here are two approaches if you want to remove the trailing 0 characters and convert to POSIX. The first uses base r, the second with tidyverse packages.

x <- c("17/04/20 17:48:04,000000000", "17/04/20 21:47:12,000000000", "15/04/20 22:19:43,000000000")

strptime(gsub("\\,[0-9]*", "", x), "%d/%m/%y %H:%M:%S", tz = "UTC")
#> [1] "2020-04-17 17:48:04 UTC" "2020-04-17 21:47:12 UTC"
#> [3] "2020-04-15 22:19:43 UTC"

lubridate::dmy_hms(stringr::str_remove(x, "\\,[0-9]*"))
#> [1] "2020-04-17 17:48:04 UTC" "2020-04-17 21:47:12 UTC"
#> [3] "2020-04-15 22:19:43 UTC"

Created on 2020-05-01 by the reprex package (v0.3.0)

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.