开发者

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 accordan开发者_开发技巧ce to polish language rules?

I want to get:

6 września 2010

and not:

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


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.


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


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'


Try this...

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜