81

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.

wjandrea
  • 23,210
  • 7
  • 49
  • 68
Alex
  • 40,671
  • 44
  • 93
  • 126

6 Answers6

98

If your application is supposed to support more than one locale then getting localized format of date/time by changing locale (by means of locale.setlocale()) is discouraged. For explanation why it's a bad idea see Alex Martelli's answer to the the question Using Python locale or equivalent in web applications? (basically locale is global and affects whole application so changing it might change behavior of other parts of application)

You can do it cleanly using Babel package like this:

>>> from datetime import date, datetime, time
>>> from babel.dates import format_date, format_datetime, format_time

>>> d = date(2007, 4, 1)
>>> format_date(d, locale='en')
u'Apr 1, 2007'
>>> format_date(d, locale='de_DE')
u'01.04.2007'

See Date and Time section in Babel's documentation.

Community
  • 1
  • 1
Piotr Dobrogost
  • 39,915
  • 36
  • 232
  • 353
88

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
mikl
  • 22,961
  • 20
  • 67
  • 89
  • 12
    BTW it won't work under Windows. Check this: http://stackoverflow.com/questions/955986/what-is-the-correct-way-to-set-pythons-locale/956084#956084 – uolot Jun 12 '09 at 08:17
  • 13
    It also requires the computer you run this on to have the locale you are trying to use generated. On GNU/Linux systems, locale -a will give you the listing of the available locales. The steps for adding new locales differ between distros. – Joakim Lundborg Jun 12 '09 at 08:29
  • 10
    Getting localized format of date/time by **changing locale is discouraged**. See my answer for the right solution. – Piotr Dobrogost May 04 '17 at 13:12
  • 2
    this breaks in python3 – mcantsin Aug 30 '20 at 02:56
  • In case the current system locale is already set to the desired locale, but still `strftime` is not honoring it, it may be due to the `datetime` module behavior. See this related [issue](https://bugs.python.org/issue29457). – amolbk Oct 19 '21 at 13:03
19

You should use %x and %X to format the date string in the correct locale. E.g. in Swedish a date is represented as 2014-11-14 instead of 11/14/2014.

The correct way to get the result as Unicode is:

locale.setlocale(locale.LC_ALL, lang)
format_ = datetime.datetime.today().strftime('%a, %x %X')
format_u = format_.decode(locale.getlocale()[1])

Here is the result from multiple languages:

Bulgarian пет, 14.11.2014 г. 11:21:10 ч.
Czech pá, 14.11.2014 11:21:10
Danish fr, 14-11-2014 11:21:10
German Fr, 14.11.2014 11:21:10
Greek Παρ, 14/11/2014 11:21:10 πμ
English Fri, 11/14/2014 11:21:10 AM
Spanish vie, 14/11/2014 11:21:10
Estonian R, 14.11.2014 11:21:10
Finnish pe, 14.11.2014 11:21:10
French ven., 14/11/2014 11:21:10
Croatian pet, 14.11.2014. 11:21:10
Hungarian P, 2014.11.14. 11:21:10
Italian ven, 14/11/2014 11:21:10
Lithuanian Pn, 2014.11.14 11:21:10
Latvian pk, 2014.11.14. 11:21:10
Dutch vr, 14-11-2014 11:21:10
Norwegian fr, 14.11.2014 11:21:10
Polish Pt, 2014-11-14 11:21:10
Portuguese sex, 14/11/2014 11:21:10
Romanian V, 14.11.2014 11:21:10
Russian Пт, 14.11.2014 11:21:10
Slovak pi, 14. 11. 2014 11:21:10
Slovenian pet, 14.11.2014 11:21:10
Swedish fr, 2014-11-14 11:21:10
Turkish Cum, 14.11.2014 11:21:10
Chinese 周五, 2014/11/14 11:21:10
schlamar
  • 8,904
  • 3
  • 36
  • 76
10

Another option is:

>>> import locale
>>> import datetime
>>> locale.setlocale(locale.LC_TIME,'')
'es_CR.UTF-8'
>>> date_format = locale.nl_langinfo(locale.D_FMT)
>>> date_format
'%d/%m/%Y'
>>> today = datetime.date.today()
>>> today
datetime.date(2012, 4, 23)
>>> today.strftime(date_format)
'23/04/2012'
BenMorel
  • 31,815
  • 47
  • 169
  • 296
Havok
  • 5,568
  • 1
  • 33
  • 43
4

solution for russian language and cross platform

import sys
import locale
import datetime

if sys.platform == 'win32':
    locale.setlocale(locale.LC_ALL, 'rus_rus')
else:
    locale.setlocale(locale.LC_ALL, 'ru_RU.UTF-8')

print(datetime.date.today().strftime("%B %Y"))

Ноябрь 2017

dEll
  • 366
  • 2
  • 7
  • have you got a solution for french language? – mee Dec 27 '18 at 12:12
  • @mee i think [this](https://stackoverflow.com/questions/955986/what-is-the-correct-way-to-set-pythons-locale-on-windows#comment27621025_956084) can help you – dEll Jan 09 '19 at 12:35
  • Getting localized format of date/time by **changing locale is discouraged**. See my answer for the right solution. – Piotr Dobrogost Sep 28 '21 at 12:12
0

This solution works in current Python 3.9 but also in Python 2.7. There is no need to mess around with local. Just use the strftime() format strings %c (date and time), %x (date only) or %X (time only):

>>> from datetime import datetime
>>> now = datetime.now()
>>> now.strftime('%c')
'So 29 Mai 2022 17:06:17 '
>>> now.strftime('%x')
'29.05.2022'
>>> now.strftime('%X')
'17:06:17'
buhtz
  • 8,057
  • 11
  • 59
  • 115