Format date with month name in polish, in python

后端 未结 3 1316
迷失自我
迷失自我 2021-01-14 01:24

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:

相关标签:
3条回答
  • 2021-01-14 01:35

    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.

    0 讨论(0)
  • 2021-01-14 01:39

    https://docs.djangoproject.com/es/1.9/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" }}

    0 讨论(0)
  • 2021-01-14 01:58

    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'
    
    0 讨论(0)
提交回复
热议问题