I would like to emphasise straight away that I am asking for the opposite of lubridate::dmy()
and lubridate::as_date()
(and similar functions), which convert strings like "31 July 2022", "July 31st, 2022", "31st of July, '22" to a standard datetime format like "2022-07-31" (as shown in the cheatsheet).
I want to do the opposite: I already have datetime variables which I wish to convert back to strings such as the above. If lubridate
can automatically detect such string formats and convert them without errors, it should be able to do the inverse as well? I couldn't figure out which function can be used for this (is there one?).
Of course, it is possible to manually do it by gluing strings from multiple functions, like so:
library(lubridate)
x <- today()
x
[1] "2022-07-31"
glue::glue("{day(x)} {month(x, label = T, abbr = F)} {year(x)}")
31 July 2022
But I was hoping there might be a function that does this directly, perhaps with a format =
specification.
CodePudding user response:
You can try the following:-
library(lubridate)
today <- today()
format(today, format = "%d %B %Y")
This will give the format that you are looking for.