Search code examples
rdatereadr

How to parse dates from a string without writing a regular expression?


readr package has a function called parse_number that returns the numbers in a string:

readr::parse_number("Hello 2022!")

[1] 2022

Is there a similar method for returning a date from a string? The readr has a function called parse_date but it does something different:

readr::parse_date("X2018-01-11_poland")

Warning: 1 parsing failure.
row col   expected             actual
  1  -- date like  X2018-01-11_poland

[1] NA

Desired output:

# the raw string is "X2018-01-11_poland"
2018-01-11

P.S. I am not interested in doing this with a regular expression.


Solution

  • The lubridate package has parse_date_time2 which is easy to use.

    library(lubridate)
    dstring <- "X2018-01-11_poland"
    date <- parse_date_time2(dstring, orders='Ymd')
    date
    #[1] "2018-01-11 UTC"