How to localize date format in Python

3 Answers

0 votes
# Localized date using Python’s built‑in locale module

import locale
from datetime import datetime

# Set locale (system default)
locale.setlocale(locale.LC_TIME, "")

now = datetime.now()

print("--- Using system locale ---")
print("Short date :", now.strftime("%x"))
print("Long date  :", now.strftime("%A, %d %B %Y"))
print("Time       :", now.strftime("%X"))
print("Weekday    :", now.strftime("%A"))
print("Month name :", now.strftime("%B"))



'''
run:

--- Using system locale ---
Short date : 04/19/26
Long date  : Sunday, 19 April 2026
Time       : 09:39:41
Weekday    : Sunday
Month name : April

'''

 



answered 15 hours ago by avibootz
0 votes
# Force a specific locale

import locale
from datetime import datetime

def try_locales(locales):
    for loc in locales:
        try:
            locale.setlocale(locale.LC_TIME, loc)
            return loc
        except locale.Error:
            continue
    return None

# French locale names
locale_used = try_locales([
    "fr_FR.UTF-8",   # Linux / macOS
    "fr_FR.utf8",
    "fr_FR",
    "French_France"  # Windows
])

if locale_used:
    print("Using locale:", locale_used)
else:
    print("No French locale installed on this system")

now = datetime.now()
print(now.strftime("%A %d %B %Y"))



'''
run:

No French locale installed on this system
Sunday 19 April 2026

'''

 



answered 14 hours ago by avibootz
0 votes
from datetime import datetime
from babel.dates import format_date

now = datetime.now()
print(format_date(now, format="full", locale="fr_FR"))

now = datetime.now()

locale = "fr_FR"  # French

print("--- Using Babel (French) ---")
print("Short date :", format_date(now, format="short", locale=locale))
print("Long date  :", format_date(now, format="full", locale=locale))
print("Time       :", format_time(now, format="medium", locale=locale))
print("Weekday    :", format_datetime(now, "EEEE", locale=locale))
print("Month name :", format_datetime(now, "MMMM", locale=locale))



'''
run:

dimanche 19 avril 2026
--- Using Babel (French) ---
Short date : 19/04/2026
Long date  : dimanche 19 avril 2026
Time       : 13:02:24
Weekday    : dimanche
Month name : avril

'''

 



answered 14 hours ago by avibootz
...