Janusz Skonieczny
Janusz Skonieczny

Reputation: 19030

Format date with month name in polish, in python

Is there an out of the box way to format in python (or within django templates), a date with full month name in accordance to polish language rules?

I want to get:

6 września 2010

and not:

>>> datetime.today().date().strftime("%d %B %Y")
'06 wrzesień 2010'

Upvotes: 7

Views: 7363

Answers (4)

Ibnu Nur Khawarizmi
Ibnu Nur Khawarizmi

Reputation: 1

Try this...

datetime.today().date().strftime("%-d %B %Y")

Upvotes: 0

Yog
Yog

Reputation: 159

https://docs.djangoproject.com/en/stable/ref/templates/builtins/#date

E
Month, locale specific alternative representation usually used for long date representation.
'listopada' (for Polish locale, as opposed to 'Listopad')

If you want to specify format in your html template then this will do:
{{ datefield|date:"j E Y" }}

Upvotes: 2

OP's code now works as he intended: returning '30 kwietnia 2020' and not 30 kwiecień 2020. Tested today with Python 2.7 and Python 3.6. Setting locale was required, I used the platform one - this might be an issue with web-apps as per other answers.

$ python
Python 2.7.17 (default, Apr 15 2020, 17:20:14) 
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> [cut repeated code]
$ python3.6
Python 3.6.9 (default, Apr 18 2020, 01:56:04) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> datetime.datetime.today()
datetime.datetime(2020, 4, 30, 18, 13, 21, 308217)
>>> datetime.datetime.today().strftime("%d %B %Y")
'30 April 2020'
>>> import locale
>>> locale.setlocale(locale.LC_ALL, '')
'pl_PL.UTF-8'
>>> datetime.datetime.today().strftime("%d %B %Y")
'30 kwietnia 2020'

Upvotes: 1

Vinay Sajip
Vinay Sajip

Reputation: 99485

Use Babel:

>>> import babel.dates
>>> import datetime
>>> now = datetime.datetime.now()
>>> print(babel.dates.format_date(now, 'd MMMM yyyy', locale='pl_PL'))
6 września 2010

Update: Incorporated Nathan Davis' comment.

Upvotes: 12

Related Questions