Format input values to time values using one of 25 preset time styles. Input
can be in the form of POSIXt
(i.e., datetimes), character
(must be in the
ISO 8601 forms of HH:MM:SS
or YYYY-MM-DD HH:MM:SS
), or Date
(which
always results in the formatting of 00:00:00
).
fmt_time(
data,
columns,
rows = everything(),
time_style = "iso",
pattern = "{x}",
locale = NULL
)
An object of class gt_tbl
.
A table object that is created using the gt()
function.
The columns to format. Can either be a series of column names
provided in c()
, a vector of column indices, or a helper function
focused on selections. The select helper functions are: starts_with()
,
ends_with()
, contains()
, matches()
, one_of()
, num_range()
, and
everything()
.
Optional rows to format. Providing everything()
(the
default) results in all rows in columns
being formatted. Alternatively,
we can supply a vector of row captions within c()
, a vector of row
indices, or a helper function focused on selections. The select helper
functions are: starts_with()
, ends_with()
, contains()
, matches()
,
one_of()
, num_range()
, and everything()
. We can also use expressions
to filter down to the rows we need (e.g.,
[colname_1] > 100 & [colname_2] < 50
).
The time style to use. By default this is "iso"
which
corresponds to how times are formatted within ISO 8601 datetime values. The
other time styles can be viewed using info_time_style()
.
A formatting pattern that allows for decoration of the
formatted value. The value itself is represented by {x}
and all other
characters are taken to be string literals.
An optional locale ID that can be used for formatting the value
according the locale's rules. Examples include "en"
for English (United
States) and "fr"
for French (France). The use of a valid locale ID will
override any values provided in sep_mark
and dec_mark
. We can use the
info_locales()
function as a useful reference for all of the locales that
are supported. Any locale
value provided here will override any global
locale setting performed in gt()
's own locale
argument.
Targeting of values is done through columns
and additionally by rows
(if
nothing is provided for rows
then entire columns are selected). Conditional
formatting is possible by providing a conditional expression to the rows
argument. See the Arguments section for more information on this.
We need to supply a preset time style to the time_style
argument. There are
many time styles and all of them can handle localization to any supported
locale. Many of the time styles are termed flexible time formats and this
means that their output will adapt to any locale
provided. That feature
makes the flexible time formats a better option for locales other than "en"
(the default locale).
The following table provides a listing of all time styles and their output
values (corresponding to an input time of 14:35:00
). It is noted which of
these represent 12- or 24-hour time.
Time Style | Output | Notes | |
1 | "iso" | "14:35:00" | ISO 8601, 24h |
2 | "iso-short" | "14:35" | ISO 8601, 24h |
3 | "h_m_s_p" | "2:35:00 PM" | 12h |
4 | "h_m_p" | "2:35 PM" | 12h |
5 | "h_p" | "2 PM" | 12h |
6 | "Hms" | "14:35:00" | flexible, 24h |
7 | "Hm" | "14:35" | flexible, 24h |
8 | "H" | "14" | flexible, 24h |
9 | "EHm" | "Thu 14:35" | flexible, 24h |
10 | "EHms" | "Thu 14:35:00" | flexible, 24h |
11 | "Hmsv" | "14:35:00 GMT+00:00" | flexible, 24h |
12 | "Hmv" | "14:35 GMT+00:00" | flexible, 24h |
13 | "hms" | "2:35:00 PM" | flexible, 12h |
14 | "hm" | "2:35 PM" | flexible, 12h |
15 | "h" | "2 PM" | flexible, 12h |
16 | "Ehm" | "Thu 2:35 PM" | flexible, 12h |
17 | "Ehms" | "Thu 2:35:00 PM" | flexible, 12h |
18 | "EBhms" | "Thu 2:35:00 in the afternoon" | flexible, 12h |
19 | "Bhms" | "2:35:00 in the afternoon" | flexible, 12h |
20 | "EBhm" | "Thu 2:35 in the afternoon" | flexible, 12h |
21 | "Bhm" | "2:35 in the afternoon" | flexible, 12h |
22 | "Bh" | "2 in the afternoon" | flexible, 12h |
23 | "hmsv" | "2:35:00 PM GMT+00:00" | flexible, 12h |
24 | "hmv" | "2:35 PM GMT+00:00" | flexible, 12h |
25 | "ms" | "35:00" | flexible |
We can use the info_time_style()
within the console to view a similar table
of time styles with example output.
Use exibble
to create a gt table. Keep only the date
and time
columns. Format the time
column to have times formatted as hms_p
(time
style 3
).
exibble %>%
dplyr::select(date, time) %>%
gt() %>%
fmt_time(
columns = time,
time_style = "h_m_s_p"
)
Use exibble
to create a gt table. Keep only the date
and time
columns. Format the time
column to have mixed time formats (times after
16:00 will be different than the others because of the expressions used
in the rows
argument).
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"
)
Use exibble
to create another gt table, this time only with the
time
column. Format the time
column to use the "EBhms"
time style
(which is one of the 'flexible' styles). Also, set the locale to "sv"
to
get the dates in Swedish.
exibble %>%
dplyr::select(time) %>%
gt() %>%
fmt_time(
columns = time,
time_style = "EBhms",
locale = "sv"
)
3-12
Other data formatting functions:
data_color()
,
fmt_bytes()
,
fmt_currency()
,
fmt_datetime()
,
fmt_date()
,
fmt_duration()
,
fmt_engineering()
,
fmt_fraction()
,
fmt_integer()
,
fmt_markdown()
,
fmt_number()
,
fmt_partsper()
,
fmt_passthrough()
,
fmt_percent()
,
fmt_roman()
,
fmt_scientific()
,
fmt()
,
sub_large_vals()
,
sub_missing()
,
sub_small_vals()
,
sub_values()
,
sub_zero()
,
text_transform()