The fmt_time() function

Let’s use the exibble dataset to create a simple, two-column gt table (keeping only the date and time columns). Format the time column with fmt_time() to display times formatted with the "h_m_s_p" time style.

exibble |>
  dplyr::select(date, time) |>
  gt() |>
  fmt_time(
    columns = time,
    time_style = "h_m_s_p"
  )
date time
2015-01-15 1:35:00 PM
2015-02-15 2:40:00 PM
2015-03-15 3:45:00 PM
2015-04-15 4:50:00 PM
2015-05-15 5:55:00 PM
2015-06-15 NA
NA 7:10:00 PM
2015-08-15 8:20:00 PM

Again using the exibble dataset, let’s format the time column to have mixed time formats, where times after 16:00 will be different than the others because of the expressions used in the rows argument. This will involve two calls of fmt_time() with different statements provided for rows. In the first call (times after 16:00) the time style "h_m_s_p" is used; for the second call, "h_m_p" is the named time style supplied to time_style.

exibble |>
  dplyr::select(date, time) |>
  gt() |>
  fmt_time(
    columns = time,
    rows = time > "16:00",
    time_style = "h_m_s_p"
  ) |>
  fmt_time(
    columns = time,
    rows = time <= "16:00",
    time_style = "h_m_p"
  )
date time
2015-01-15 1:35 PM
2015-02-15 2:40 PM
2015-03-15 3:45 PM
2015-04-15 4:50:00 PM
2015-05-15 5:55:00 PM
2015-06-15 NA
NA 7:10:00 PM
2015-08-15 8:20:00 PM

Use the exibble dataset to create a single-column gt table (with only the time column). Format the time values using the "EBhms" time style (which is one of the ‘flexible’ styles). Also, we’ll set the locale to "sv" to get the times in Swedish.

exibble |>
  dplyr::select(time) |>
  gt() |>
  fmt_time(
    columns = time,
    time_style = "EBhms",
    locale = "sv"
  )
time
tors 1:35:00 på efterm.
tors 2:40:00 på efterm.
tors 3:45:00 på efterm.
tors 4:50:00 på efterm.
tors 5:55:00 på efterm.
NA
tors 7:10:00 på kvällen
tors 8:20:00 på kvällen