Localising dates and numbers

Use Python’s locale module in client code to display dates, numbers and currency amounts using a language and region’s conventions.

Choose a locale

Select a locale for the type of information you want to format:

Category Controls
locale.LC_TIME Date and time formatting, parsing and DatePickers
locale.LC_NUMERIC Number formatting and parsing
locale.LC_MONETARY Currency formatting

For example, select German date conventions before creating your Forms or DatePickers:

Python | Client
import locale
from datetime import date

locale.setlocale(locale.LC_TIME, "de_DE")

value = date(2026, 3, 9)
print(value.strftime("%A, %d. %B %Y"))
# Montag, 09. März 2026

Locale names include a language and, optionally, a region. Both "de_DE" and "de-DE" select German conventions for Germany; "de_CH" selects German conventions for Switzerland.

Pass an empty string to use the browser’s default locale:

Python | Client
locale.setlocale(locale.LC_TIME, "")

Each category has its own setting. Selecting German dates does not also select German number or currency formatting. These settings apply throughout the client code in the current app; they do not change the locale in Server Modules.

The initial locale is "C", which uses English date names and a decimal point for numbers. You can return a category to its defaults with locale.setlocale(category, "C").

Localise a DatePicker

DatePickers follow LC_TIME. Set the locale before creating the picker:

Python | Client
import locale
from datetime import date
from anvil import DatePicker

locale.setlocale(locale.LC_TIME, "de_DE")
picker = DatePicker(date=date(2026, 3, 9))
# Displays 09.03.2026

If you added DatePickers in the designer, put the setlocale() call in your Form’s code before the class definition.

Leave format unset to use the locale’s default date format, or date-and-time format when pick_time=True. You can also set an explicit Python date format, such as format="%d %B %Y", to display 09 März 2026.

The DatePicker’s month and weekday names also follow the selected locale. On supported browsers, the locale also determines the first day of the week shown in the DatePicker: for example, Monday for German or British English, and Sunday for US English.

Localising a date changes how it is displayed, not its timezone. See Dealing with Timezones for timezone handling.

Choose the first day of the week

Append -u-fw-sun to a locale name to request Sunday first while keeping its date format and language. For example, use German dates with a Sunday-first DatePicker:

Python | Client
import locale
from anvil import DatePicker

locale.setlocale(locale.LC_TIME, "de-DE-u-fw-sun")
picker = DatePicker()

This uses a Unicode locale extension for choosing the first day of the week, supported by the browser. The extended locale name is specific to client code; it is not portable to Server Modules or other Python environments.

Use -u-fw-mon for Monday first. Other day values are tue, wed, thu, fri and sat. Select the locale before creating your Forms or DatePickers.

Format and parse numbers

Select LC_NUMERIC to control decimal separators and digit grouping:

Python | Client
import locale

locale.setlocale(locale.LC_NUMERIC, "de_DE")

print(locale.format_string("%.2f", 1234.5, grouping=True))
# 1.234,50

print(locale.atof("1.234,50"))
# 1234.5

Use locale.format_string() when you want localised number formatting, rather than interpolating the value directly.

Format currency

Select LC_MONETARY before calling locale.currency():

Python | Client
import locale

locale.setlocale(locale.LC_MONETARY, "de_DE")

print(locale.currency(1234.5, grouping=True))
# 1.234,50 €

The locale’s region determines the default currency: for example, "de_DE" uses euros, while "de_CH" uses Swiss francs. Include the region when it matters, since a language-only name uses the browser’s inferred region. The browser supplies the currency symbol, fractional digits and formatting conventions.

The "C" locale has no default currency, so locale.currency() raises ValueError until you select a monetary locale.

Browser support

Browser locale API availability varies by feature:

Feature Widely supported since
Date/time localisation (LC_TIME) October 2018
Number localisation (LC_NUMERIC) September 2019
Currency localisation (LC_MONETARY) September 2020

Individual browsers may support these features in earlier versions.

Available locales and exact formatting depend on the user’s browser. For example, different browser versions may abbreviate September as Sep or Sept in British English.

Dates use the Gregorian calendar and Latin digits. Client code supports LC_TIME, LC_NUMERIC and LC_MONETARY; selecting other categories, including LC_ALL, raises NotImplementedError.

If a browser cannot support the requested category, setlocale() raises NotImplementedError. An unavailable locale name raises locale.Error. Both leave the previous selection unchanged.

English dates and C-locale number formatting work without browser locale support. Currency formatting needs browser support for regional currency conventions.

See Python’s locale reference for the formatting and parsing functions.

First day of the week support

Choosing the first day of the week in a DatePicker requires newer browser support than translating dates. This feature became widely available across major browsers in July 2026, with earlier support in some browsers.

On browsers without this support, DatePickers use Sunday first. Setting the locale and translating dates still work on browsers with date/time localisation support.


Do you still have questions?

Our Community Forum is full of helpful information and Anvil experts.