How to ouput a date punctually in another language in R

Viewed 113

I usually set my working environment in English although it is not my mother tongue, because most dates I use in input are this way.

However, I sometimes to output a date in a long format in another language, usually French.

library(lubridate) 
today()
> today()
[1] "2020-12-22"

That is, I would like a function that gives :

DateInLongFormatInFrench(today)
[1] "Mardi 22 décembre"

Which is the French for "Tuesday, December the 22th". And I would like the solution not to change the language setting for the whole program, but just this instance.

I have found a lot of post on how to read dates, but not so much on how to output them

1 Answers

Convert dates to strings using format.Date

format(<my_date>, "%A %d %B")

You must have your locale set to a Francophone region, e.g.

Sys.setlocale("LC_TIME", "fr_FR")

If you need to do this in isolation:

frenchDate <- function(x) {
  locale <- Sys.getlocale("LC_TIME")
  
  # when function exits, restore original locale
  on.exit(Sys.setlocale("LC_TIME", locale))

  Sys.setlocale("LC_TIME", "fr_FR")

  format(x, "%A %d %B")
}
Related