Search code examples
ras.date

Convert date with 'May' to number format doesn't work in R


I am wondering why the following works

as.Date("07Jan2013", "%d%B%Y")
"2013-01-07"

but, this doesn't

as.Date("07May2013", "%d%B%Y")
NA

Only the month May gives this issue.


Solution

  • This is due to local date. Please try this code from as.Date help :

    lct <- Sys.getlocale("LC_TIME")
    Sys.setlocale("LC_TIME", "C")
    x <- "07May2013"
    z <- as.Date(x, "%d%B%Y")
    Sys.setlocale("LC_TIME", lct) #set locale back
    z
    > "2013-05-07"
    

    I suppose that as.Date("07Jan2013", "%d%B%Y")is working because Jan is Janvier in French, same prefix than January