Localising dates and numbers

Dates, numbers and currency amounts are displayed differently around the world. For example, some regions use DD/MM/YYYY for dates, while others use MM/DD/YYYY. Number separators and currency symbols also vary between regions.

In Anvil, you can use Python’s locale module in client code to display these values using a language and region’s conventions. It also controls how DatePickers display dates and which day the calendar starts on.

How locale settings work

Locale categories

The locale module provides separate categories for formatting dates, numbers and currency amounts. Each category is configured independently:

  • locale.LC_TIME: Date and time formatting, date parsing and DatePicker localisation
  • locale.LC_NUMERIC: Number formatting, decimal separators and number parsing
  • locale.LC_MONETARY: Currency formatting, including currency symbols and monetary conventions

Anvil supports only these three locale categories. The standard Python LC_ALL category, which sets all categories at once, is not supported. Set each category you need individually.

For example, setting LC_TIME to German changes how dates are formatted, but numbers keep their default formatting:

Python | Client
import locale
from datetime import date

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

# Check which locale LC_TIME is using
print(locale.getlocale(locale.LC_TIME))
# ('de_DE', 'UTF-8')

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

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

Locale names

A locale name starts with a language code and can include a region code. For example, "de_DE" selects German as used in Germany, and "de_CH" selects German as used in Switzerland.

You can separate the language and region with an underscore or a hyphen, so "de_DE" and "de-DE" are equivalent. If the browser doesn’t recognise a locale name, setlocale() raises locale.Error and keeps the previous locale.

Until you call setlocale(), every category uses the "C" locale. This gives English month and day names, US-style month/day/year dates, and a decimal point for numbers.

To use the locale the user’s browser is set to, pass an empty string:

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

To switch a category back to the "C" locale, pass "C":

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

Setting the locale

Locale settings apply throughout the client code in the current app, but not in Server Modules. DatePickers use these settings when they are initialised, so set the locale before creating your Forms to ensure they use the intended settings. You can do this in a Startup Module, before opening your first Form, or at the top of your Startup Form, before the class definition.

For example, the following Startup Form sets the date, number and currency conventions to German as used in Germany:

Python | Client
 
import locale

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

# Class definition
class Form1(Form1Template):
  def __init__(self, **properties):
    self.init_components(**properties)

Format dates

LC_TIME controls date and time formatting, including how dates are displayed in DatePickers. However, dates always use the Gregorian calendar and Latin digits, regardless of the selected locale.

If your app used Moment.js locale scripts to translate DatePickers, see Moment localisation warnings to switch to Python’s locale module.

Localise a DatePicker

For example, to display German dates in a DatePicker, 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

A DatePicker’s format property controls how its date is displayed. Leave format unset to use the locale’s default date format, or its default date-and-time format when pick_time is enabled.

To use a custom format instead, set format in the Properties Panel or in code, using Python’s date format codes. Month and weekday names still follow the selected locale:

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), format="%d %B %Y")
# Displays 09 März 2026

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

On supported browsers, the locale also determines the first day of the week shown in the DatePicker. For example, German and British English calendars start on Monday, while US English calendars start on Sunday. On other browsers, DatePickers use Sunday first.

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()

Use -u-fw-mon for Monday first. Other day values are tue, wed, thu, fri and sat. This is a Unicode locale extension that the browser interprets, so it only works in client code.

Format and parse numbers

LC_NUMERIC is used to format numbers according to the selected locale, including decimal separators and digit grouping. Set it before formatting or parsing numbers:

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() to format numbers according to the selected locale and locale.atof() to parse a localised number string back into a numeric value. See Python’s locale reference for other formatting and parsing functions.

Format currency

Use LC_MONETARY to format currency amounts according to the selected locale. Set it 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. If you use a language-only name, such as "de", the browser’s inferred region determines the default currency.

locale.currency() formats the amount using the locale’s default currency but it does not convert the amount between currencies.

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

Browser support

Localising dates, numbers and currency works in all current major browsers. The exception is choosing the first day of the week in a DatePicker, which has been widely available since July 2026.

On older browsers without localisation support, setlocale() raises NotImplementedError, but English dates and the "C" locale still work. To check whether a particular browser supports localisation, see MDN’s compatibility data for dates, numbers and currency.


Do you still have questions?

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