Locale date formatting in Python

Alex picture Alex · Jun 12, 2009 · Viewed 84.5k times · Source

How do I get datetime.datetime.now() printed out in the native language?

    >>> session.deathDate.strftime("%a, %d %b %Y")
    'Fri, 12 Jun 2009'

I'd like to get the same result but in local language.

Answer

mikl picture mikl · Jun 12, 2009

You can just set the locale like in this example:

>>> import time
>>> print time.strftime("%a, %d %b %Y %H:%M:%S")
Sun, 23 Oct 2005 20:38:56
>>> import locale
>>> locale.setlocale(locale.LC_TIME, "sv_SE") # swedish
'sv_SE'
>>> print time.strftime("%a, %d %b %Y %H:%M:%S")
sön, 23 okt 2005 20:39:15