Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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.


Solution 2:

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" }}


Solution 3:

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'

Post a Comment for "Format Date With Month Name In Polish, In Python"