Depending on your use case you may or may not want to alter your locales. If the latter applies, lubridate
parsers allow you to set locale
just for the function, without changing your environment:
lubridate::mdy("October 15, 2018", locale = "C")
#> [1] "2018-10-15"
Or if it's part of data import, perhaps consider readr
, its default locale handling is quite convenient on non-US systems:
library(readr)
# system lcoale:
Sys.getlocale()
#> [1] "LC_COLLATE=Estonian_Estonia.utf8;LC_CTYPE=Estonian_Estonia.utf8;LC_MONETARY=Estonian_Estonia.utf8;LC_NUMERIC=C;LC_TIME=Estonian_Estonia.utf8"
# E.g. for "%B %d, %Y" to work we would need "juuli" instead of "July":
format(Sys.time(), "%B %d, %Y")
#> [1] "juuli 24, 2023"
# readr::default_locale(), will be used by readr::parse_date() :
default_locale()
#> <locale>
#> Numbers: 123,456.78
#> Formats: %AD / %AT
#> Timezone: UTC
#> Encoding: UTF-8
#> <date_names>
#> Days: Sunday (Sun), Monday (Mon), Tuesday (Tue), Wednesday (Wed), Thursday
#> (Thu), Friday (Fri), Saturday (Sat)
#> Months: January (Jan), February (Feb), March (Mar), April (Apr), May (May),
#> June (Jun), July (Jul), August (Aug), September (Sep), October
#> (Oct), November (Nov), December (Dec)
#> AM/PM: AM/PM
# import CSV that uses foreign date format for that specific system locale,
# without changing any locale settings:
dummy_csv <- '"October 15, 2018"';
read_csv(I(dummy_csv), col_names = FALSE, col_types = cols(col_date(format = "%B %d, %Y")))
#> # A tibble: 1 × 1
#> X1
#> <date>
#> 1 2018-10-15