Dates and times are some of the most ambiguous values a table can show. Is “03/04/2025” the 3rd of April or the 4th of March? Is “18:00” the same as “6:00 PM”? The answer depends on where the reader lives, and a reader should never have to guess. Great Tables has formatters for dates, times, date-times and durations, each with a set of styles and support for the conventions of many locales.
This page follows Formatting Values, which covers numbers.
Date Styles
There are 41 date styles, and they come in two kinds:
- Fixed styles have descriptive names like
"day_month_year" or "wd_m_day_year". The name says which parts appear and in what order, and that order is the same in every locale.
- Flexible styles are named with CLDR skeleton codes, like
"yMMMd" or "MMMd". The code says which parts appear (y for the year, MMM for an abbreviated month, d for the day, E for the weekday), and the locale decides their order and punctuation.
Here are some of each, applied to the same date:
import polars as pl
import polars.selectors as cs
styles = [
"iso", "wday_month_day_year", "wd_m_day_year", "month_day_year", "m_day_year",
"day_m_year", "day_month_year", "day_month", "year_quarter",
"yMd", "yMMMd", "yMMMEd", "yMMM", "yMMMM", "MMMd", "MMMEd", "yQQQ",
]
style_df = pl.DataFrame({
"date_style": styles,
"kind": ["fixed"] * 9 + ["flexible"] * 8,
"output": [vals.fmt_date("2000-02-29", date_style=s)[0] for s in styles],
})
(
GT(style_df, rowname_col="date_style", groupname_col="kind")
.tab_stubhead(label="date_style=")
.cols_label(output="2000-02-29")
)
| date_style= |
2000-02-29 |
| fixed |
| iso |
2000-02-29 |
| wday_month_day_year |
Tuesday, February 29, 2000 |
| wd_m_day_year |
Tue, Feb 29, 2000 |
| month_day_year |
February 29, 2000 |
| m_day_year |
Feb 29, 2000 |
| day_m_year |
29 Feb 2000 |
| day_month_year |
29 February 2000 |
| day_month |
29 February |
| year_quarter |
2000-Q1 |
| flexible |
| yMd |
2/29/2000 |
| yMMMd |
Feb 29, 2000 |
| yMMMEd |
Tue, Feb 29, 2000 |
| yMMM |
Feb 2000 |
| yMMMM |
February 2000 |
| MMMd |
Feb 29 |
| MMMEd |
Tue, Feb 29 |
| yQQQ |
Q1 2000 |
The reference page for fmt_date() lists all 41. Names that aren’t in that list, such as "long" or "short", raise an error. When you need a date without its year, "day_month" ("29 February"), "MMMd" ("Feb 29") and "MMMMd" ("February 29") are the styles to reach for.
Dates in Other Locales
Set a locale with GT(locale=...) for the whole table, or with the locale= argument of fmt_date() for one call, and the month and weekday names appear in that language.
The two kinds of style behave differently here. A fixed style keeps its order whatever the locale, which suits a table that must look the same everywhere but can read oddly: "month_day_year" in German gives “Februar 29, 2000”, where a German reader expects “29. Februar 2000”. A flexible style follows the locale’s own conventions.
locales = ["en", "de", "fr", "es", "ja"]
locale_df = pl.DataFrame({
"locale": locales,
"month_day_year": [
vals.fmt_date("2000-02-29", date_style="month_day_year", locale=loc)[0] for loc in locales
],
"yMMMd": [vals.fmt_date("2000-02-29", date_style="yMMMd", locale=loc)[0] for loc in locales],
})
GT(locale_df, rowname_col="locale").tab_spanner(label="date_style=", columns=cs.all())
|
date_style=
|
| month_day_year |
yMMMd |
| en |
February 29, 2000 |
Feb 29, 2000 |
| de |
Februar 29, 2000 |
29. Feb. 2000 |
| fr |
février 29, 2000 |
29 févr. 2000 |
| es |
febrero 29, 2000 |
29 feb 2000 |
| ja |
2月 29, 2000 |
2000年2月29日 |
So for a table in a single locale other than English, either choose a fixed style whose order matches that locale (a day-first style like "day_month_year" for German or French) or use a flexible style and let the locale decide.
Date-Times
The fmt_date() and fmt_time() methods each handle half of a timestamp. For values holding both a date and a time, fmt_datetime() takes a date_style= and a time_style= together and joins the two parts with sep=.
from datetime import datetime
datetime_df = pl.DataFrame({
"event": ["Doors open", "Keynote", "Reception"],
"starts": [
datetime(2024, 9, 16, 8, 30),
datetime(2024, 9, 16, 9, 15),
datetime(2024, 9, 17, 18, 0),
],
})
(
GT(datetime_df, rowname_col="event")
.fmt_datetime(columns="starts", date_style="wd_m_day_year", time_style="h_m_p")
)
|
starts |
| Doors open |
Mon, Sep 16, 2024 8:30 AM |
| Keynote |
Mon, Sep 16, 2024 9:15 AM |
| Reception |
Tue, Sep 17, 2024 6:00 PM |
The style keywords are the same ones used by fmt_date() and fmt_time(), so a table that mixes date, time, and date-time columns can present them consistently.
When no combination of styles gives the form you want, format_str= takes a format string instead. It can be a CLDR pattern, which is localized ("EEEE, d MMMM y 'at' HH:mm"), or a strftime() string containing % ("%d.%m.%Y %H:%M"), which isn’t.
(
GT(datetime_df, rowname_col="event")
.fmt_datetime(columns="starts", format_str="EEEE d MMMM, HH:mm")
)
|
starts |
| Doors open |
Monday 16 September, 08:30 |
| Keynote |
Monday 16 September, 09:15 |
| Reception |
Tuesday 17 September, 18:00 |
Durations
The fmt_duration() method formats numeric values (or timedelta objects) as styled time duration strings. You specify the input unit and the method handles the conversion and display.
duration_df = pl.DataFrame({
"event": ["Sprint", "Marathon", "Triathlon", "Ultra"],
"seconds": [58, 7_380, 21_900, 187_265],
})
(
GT(duration_df, rowname_col="event")
.fmt_duration(columns="seconds", input_units="seconds")
)
|
seconds |
| Sprint |
58s |
| Marathon |
2h 3m |
| Triathlon |
6h 5m |
| Ultra |
2d 4h 1m 5s |
The duration_style= argument controls the output format. The available styles are:
"narrow" (the default): compact format like "2d 3h 15m"
"wide": spelled out like "2 days 3 hours 15 minutes"
"colon-sep": clock format like "2/03:15:00"
"iso": ISO 8601 format like "P2DT3H15M"
(
GT(duration_df, rowname_col="event")
.fmt_duration(columns="seconds", input_units="seconds", duration_style="wide")
)
|
seconds |
| Sprint |
58 seconds |
| Marathon |
2 hours 3 minutes |
| Triathlon |
6 hours 5 minutes |
| Ultra |
2 days 4 hours 1 minute 5 seconds |
You can limit the number of output units with max_output_units= to keep the display concise.
(
GT(duration_df, rowname_col="event")
.fmt_duration(columns="seconds", input_units="seconds", max_output_units=2)
)
|
seconds |
| Sprint |
58s |
| Marathon |
2h 3m |
| Triathlon |
6h 5m |
| Ultra |
2d 4h |
Limiting the output to two units (e.g., "2d 4h" instead of "2d 4h 1m 5s") keeps the display compact when exact precision is not required. To choose the units yourself, give output_units= a list such as ["hours", "minutes"]; larger amounts are then expressed in the largest unit listed, and amounts below the smallest one are shown as less than one of it ("<1m").
(
GT(duration_df, rowname_col="event")
.fmt_duration(columns="seconds", input_units="seconds", output_units=["hours", "minutes"])
)
|
seconds |
| Sprint |
<1m |
| Marathon |
2h 3m |
| Triathlon |
6h 5m |
| Ultra |
52h 1m |
By default, units with a value of zero are left out (trim_zero_units=True), so 3,600 seconds is "1h" rather than "1h 0m 0s".