Extra date formats

I'm working on analysis for a very non-technical audience across continents used to different date formats, and I keep wishing lubridate had an option to present dates in a unambiguous format such as DDMMMYYYY (eg. 01JAN2026) or ddmmmyyyy (01Jan2026). It's mostly so we're not constantly guessing what date we're talking about. I'm also working with some old SAS programmers who are like "Oh SAS can just present it this way, how do I do this in R?"

I don't know if it's the right use of lubridate?

I currently wrote myself a function to create a new column with this format (stored as character), but I will need to keep the old one to do actual calculations.

# Function to create unambiguous date columns in DDMMMYYYY format (e.g. 01JAN2026)

add_ddmmmyyyy <- function(data, input_dates) {
  date_cols <- data |>
    dplyr::select({{ input_dates }}) |>
    names()

  data |>
    dplyr::mutate(
      dplyr::across(
        dplyr::all_of(date_cols),
        \(x) stringr::str_c(
          stringr::str_pad(lubridate::day(x), width = 2, pad = "0"),
          stringr::str_to_upper(month.abb[lubridate::month(x)]),
          lubridate::year(x)
        ),
        .names = "{.col}_coded"
      )
    ) |>
    purrr::reduce(
      date_cols,
      \(df, col) dplyr::relocate(
        df,
        dplyr::all_of(paste0(col, "_coded")),
        .after = dplyr::all_of(col)
      ),
      .init = _
    )
}

#Example
dataframe <- tibble::tribble(
  ~startdate,    ~event,      ~enddate,
  "2026-01-01",  "travel",    "2026-03-01",
  "2026-01-31",  "leave",     "2026-03-31"
) |> dplyr::mutate(
  startdate = as.Date(startdate),
  enddate = as.Date(enddate)
)

dataframe


add_ddmmmyyyy(dataframe, c(startdate, enddate))

First, I think many consider the "unambiguous" date format to be YYYY-MM-DD, i.e. 2026-10-27.

format(lubridate::mdy("01/02/2026"))
# [1] "2026-01-02"

Second, formatting as 01Jan2026 is fairly straightforward:

format(lubridate::mdy("01/02/2026"), "%d%b%Y")
# [1] "02Jan2026"

Hope this helps!

Agreed -- after I was introduced to ISO 8601's "yyyy-mm-dd" format, it's become the main thing I use, but like @kevbaer says it's easy to format a date column to print in your preferred order. :slight_smile: