Home > Net >  Convert date without leading zeros in lubridate
Convert date without leading zeros in lubridate

Time:11-30

I have dates with the format

"7-10-2013 10:18"

However, when I try to use lubridate::as_date with them, I get this warning

Warning message:
All formats failed to parse. No formats found.

I think this is due to the fact that the date has no leading zeros. How can I use lubridate in this case?

CodePudding user response:

You can pass along the date-time format as a vector to parse_date_time

library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union

dat <- '7-10-2013 10:18'

parse_date_time(dat, "mdy HM")
#> [1] "2013-07-10 10:18:00 UTC"

Created on 2022-11-29 with reprex v2.0.2

CodePudding user response:

We can use Dirk Eddelbuettel's anytime package:

anytime::anytime("7-10-2013 10:18")

 # [1] "2013-07-10 10:18:00 EDT"

or simply in base:

strptime("7-10-2013 10:18", format = "%m-%d-%Y %H:%M")
  • Related