Skip to main content

Formatting Dates and Times

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.

Formatting Dates

The fmt_date() method formats date values with a style chosen through date_style=. Let’s take a few rows of the exibble dataset, whose date, time and datetime columns hold ISO 8601 text.

from great_tables import GT, data, vals

gt_ex = GT(data.pl.exibble.select("date", "time", "datetime").head(5))

gt_ex
date time datetime
2015-01-15 13:35 2018-01-01 02:22
2015-02-15 14:40 2018-02-02 14:33
2015-03-15 15:45 2018-03-03 03:44
2015-04-15 16:50 2018-04-04 15:55
2015-05-15 17:55 2018-05-05 04:00

The ISO form (2015-01-15) is unambiguous but not easy to read. Let’s give the date column a style that spells out the month.

gt_ex.fmt_date(columns="date", date_style="m_day_year")
date time datetime
Jan 15, 2015 13:35 2018-01-01 02:22
Feb 15, 2015 14:40 2018-02-02 14:33
Mar 15, 2015 15:45 2018-03-03 03:44
Apr 15, 2015 16:50 2018-04-04 15:55
May 15, 2015 17:55 2018-05-05 04:00

The values to format can be ISO 8601 strings (as here), Python date or datetime objects, or the date and datetime types of Polars and Pandas. A pattern= argument decorates the result, so pattern="({x})" puts each date in parentheses.

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.

Formatting Times

The fmt_time() method works like fmt_date(), with a time_style= argument. There are 25 time styles, again in fixed and flexible kinds. The fixed styles include "iso" (14:35:00), "iso-short" (14:35), "h_m_s_p" (2:35:00 PM), "h_m_p" (2:35 PM) and "h_p" (2 PM). In the flexible styles, H is the hour on the 24-hour clock and h the hour on the 12-hour clock.

time_styles = ["iso", "iso-short", "h_m_s_p", "h_m_p", "h_p", "Hms", "Hm", "hms", "hm", "Ehm"]

time_df = pl.DataFrame({
    "time_style": time_styles,
    "en": [vals.fmt_time("2000-02-29T14:35:00", time_style=s)[0] for s in time_styles],
    "de": [vals.fmt_time("2000-02-29T14:35:00", time_style=s, locale="de")[0] for s in time_styles],
})

GT(time_df, rowname_col="time_style").tab_stubhead(label="time_style=")
time_style= en de
iso 14:35:00 14:35:00
iso-short 14:35 14:35
h_m_s_p 2:35:00 PM 2:35:00 PM
h_m_p 2:35 PM 2:35 PM
h_p 2 PM 2 PM
Hms 14:35:00 14:35:00
Hm 14:35 14:35
hms 2:35:00 PM 2:35:00 PM
hm 2:35 PM 2:35 PM
Ehm Tue 2:35 PM Di. 2:35 PM

In German, only the weekday name changes: the 12-hour styles still end in “AM” or “PM”. Many locales use the 24-hour clock by default, so for a table aimed at one, a 24-hour style like "Hm" or "iso-short" is usually the better choice.

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".

Formatting Dates and Times Outside a Table

As with numbers, the vals module has functional versions of these formatters that work on single values or lists. They’re a quick way to try out a style before using it in a table, and they’re how the style tables above were built.

fmt_date_1 = vals.fmt_date(
    ["2015-03-15", "2017-08-18", "2020-04-12"], date_style="wday_month_day_year"
)
fmt_date_2 = vals.fmt_date(["2015-03-15", "2017-08-18", "2020-04-12"], date_style="month_day_year")
fmt_date_3 = vals.fmt_date(
    ["2015-03-15", "2017-08-18", "2020-04-12"], date_style="yMMMd", locale="fr"
)
fmt_time_1 = vals.fmt_time(["23:03", "00:55", "08:23"], time_style="h_m_p")
fmt_time_2 = vals.fmt_time(["23:03", "00:55", "08:23"], time_style="h_p")

print(fmt_date_1, fmt_date_2, fmt_date_3, fmt_time_1, fmt_time_2, sep="\n")
['Sunday, March 15, 2015', 'Friday, August 18, 2017', 'Sunday, April 12, 2020']
['March 15, 2015', 'August 18, 2017', 'April 12, 2020']
['15 mars 2015', '18 août 2017', '12 avr. 2020']
['11:03 PM', '12:55 AM', '8:23 AM']
['11 PM', '12 AM', '8 AM']

Pick a date style for what the reader needs (the full date, the month and year, or only the day and month) and for where the reader is. Fixed styles keep one order everywhere; flexible styles adapt to the locale. For times, choose between the 12-hour and 24-hour clocks to match your readers, and for durations, show only as many units as the comparison needs. The next page, More Formatting Options, covers the remaining formatters, from byte sizes and units to flags, images and custom functions.