2

My session is in Spanish. When I have a format with full month name (%B) I get the correct date:

as.Date("01-Febrero-2021", format = "%d-%B-%Y")
# [1] "2021-02-01"

However, when I try to use the abbreviated month (%b), I get an "NA":

as.Date("01-Feb-2021", format = "%d-%b-%Y")
# [1] NA

as.Date("01-feb-2021", format = "%d-%b-%Y")
# [1] NA

as.Date("01-FEB-2021", format = "%d-%b-%Y")
# [1] NA

What I'm doing wrong?

Thanks to the answer from G. Grothendieck, I could make it work:

Sys.getlocale("LC_TIME")
# [1] "Spanish_Argentina.1252"

When checking the abbreviated month using format on Sys.Date, it turns out that the month is written with a period:

format(Sys.Date(), "%b")
# [1] "feb."

Try to parse a string, this time with a period:

as.Date("01-feb.-2021", format = "%d-%b-%Y")
# [1] "2021-02-01"

Works!

Henrik
  • 65,555
  • 14
  • 143
  • 159
esterodr
  • 117
  • 1
  • 7
  • Related post where they also had problems with a period (`.`) after the abbreviated month (`%b`) when using locale `"Spanish_Chile.1252"`: [Problem with as.Date %b format in short month name writen with dot R](https://stackoverflow.com/questions/52783053/problem-with-as-date-b-format-in-short-month-name-writen-with-dot-r). Locale is a nightmare! – Henrik Feb 21 '21 at 17:53

1 Answers1

1

It works for me. Make sure you are actually in a Spanish locale. (I am on Windows and it is possible that it works differently on other platforms.)

Sys.setlocale("LC_TIME", "English")
## [1] "English_United States.1252"
Sys.getlocale("LC_TIME")
## [1] "English_United States.1252"

format(Sys.Date(), "%b")
## [1] "Feb"

Now change to Spanish:

Sys.setlocale("LC_TIME", "Spanish")
## [1] "Spanish_Spain.1252"
Sys.getlocale("LC_TIME")
## [1] "Spanish_Spain.1252"

format(Sys.Date(), "%b")
## [1] "feb"

as.Date("01-feb-2021", format="%d-%b-%Y")
## [1] "2021-02-01"
as.Date("01-Feb-2021", format="%d-%b-%Y")
## [1] "2021-02-01"
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • 1
    Thanks!! Your answer helped me. I have to use "feb." or "Feb.", with a dot at the end. I edited my question to explain this. – esterodr Feb 21 '21 at 17:52