The fmt_date() function

Let’s use the exibble dataset to create a simple, two-column gt table (keeping only the date and time columns). With fmt_date(), we’ll format the date column to display dates formatted with the "month_day_year" date style.

exibble |>
  dplyr::select(date, time) |>
  gt() |>
  fmt_date(
    columns = date,
    date_style = "month_day_year"
  )
date time
January 15, 2015 13:35
February 15, 2015 14:40
March 15, 2015 15:45
April 15, 2015 16:50
May 15, 2015 17:55
June 15, 2015 NA
NA 19:10
August 15, 2015 20:20

Again using the exibble dataset, let’s format the date column to have mixed date formats, where dates after April 1st will be different than the others because of the expressions used in the rows argument. This will involve two calls of fmt_date() with different statements provided for rows. In the first call (dates after the 1st of April) the date style "m_day_year" is used; for the second call, "day_m_year" is the named date style supplied to date_style.

exibble |>
  dplyr::select(date, time) |>
  gt() |>
  fmt_date(
    columns = date,
    rows = as.Date(date) > as.Date("2015-04-01"),
    date_style = "m_day_year"
  ) |>
  fmt_date(
    columns = date,
    rows = as.Date(date) <= as.Date("2015-04-01"),
    date_style = "day_m_year"
  )
date time
15 Jan 2015 13:35
15 Feb 2015 14:40
15 Mar 2015 15:45
Apr 15, 2015 16:50
May 15, 2015 17:55
Jun 15, 2015 NA
NA 19:10
Aug 15, 2015 20:20

Use the exibble dataset to create a single-column gt table (with only the date column). Format the date values using the "yMMMEd" date style (which is one of the ‘flexible’ styles). Also, we’ll set the locale to "nl" to get the dates in Dutch.

exibble |>
  dplyr::select(date) |>
  gt() |>
  fmt_date(
    date_style = "yMMMEd",
    locale = "nl"
  )
date
do 15 jan. 2015
zo 15 feb. 2015
zo 15 mrt. 2015
wo 15 apr. 2015
vr 15 mei 2015
ma 15 jun. 2015
NA
za 15 aug. 2015