Search code examples
django-cms

Aldryn_newsblog custom publishing date format


Article publishing date format is "Mon dd, yyyy" I'd like it to be "dd.mm.yyyy". The code, that causes different format is here

{{ article.publishing_date|date }}

Solution

  • You should localise your project with a formats file so you can set defaults for things like this.

    There is a setting to tell django where your formats live;

    FORMAT_MODULE_PATH = [
        'project.formats'
    ]
    

    Docs for that are here; https://docs.djangoproject.com/en/2.2/ref/settings/#format-module-path

    As an example, the above formats files lives in a path like project/formats/en/formats.py and contain the formats for that language;

    DATE_FORMAT = 'N j, Y'
    TIME_FORMAT = 'P'
    DATETIME_FORMAT = 'N j, Y, P'
    YEAR_MONTH_FORMAT = 'F Y'
    MONTH_DAY_FORMAT = 'F j'
    SHORT_DATE_FORMAT = 'm/d/Y'
    SHORT_DATETIME_FORMAT = 'm/d/Y P'
    
    FIRST_DAY_OF_WEEK = 0  # Sunday
    
    US_DATE_FORMAT = '%m/%d/%Y'
    UK_DATE_FORMAT = '%d/%m/%Y'
    
    DECIMAL_SEPARATOR = '.'
    THOUSAND_SEPARATOR = ','
    NUMBER_GROUPING = 3
    

    Docs on creating your own formats are here; https://docs.djangoproject.com/en/2.2/topics/i18n/formatting/#creating-custom-format-files

    You can make use of the formats you define by importing django.utils.formats.get_format then doing get_format('DECIMAL_SEPARATOR') or...

    input_format = get_format('US_DATE_FORMAT')
    v = datetime.datetime.strptime(
        value, input_format
    )