Mauricio Machuca
Mauricio Machuca

Reputation: 115

How to get a localized date on Django template using a specific format?

I am having problems to get a localized date on Django templates.

I have this in my config file:

LOCALE_NAME ='es_VE'

LANGUAGE_CODE = 'es-ve'

TIME_ZONE = 'America/Caracas'

USE_I18N = True

USE_L10N = True

USE_TZ = True

The date I want to render is : mydate=2016-03-21 23:59:59.999999-04:30

This code {{mydate|date:"d/m/Y"}} shows "21/03/2016"

and this one {{mydate|localize}} shows "22 de Marzo de 2016 a las 04:29" (22/03/2016).

I want to get the result of the localize filter using the date's filter format.

Is there a way to achieve that?

Upvotes: 5

Views: 2897

Answers (2)

Mauricio Machuca
Mauricio Machuca

Reputation: 115

I ended using a custom template filter:

@register.filter
@stringfilter
def datedate(value,format="%d/%m/%Y"):
    try:
        return parse_datetime(value).strftime(format)
    except Exception as e:
        return ""

So when I did {{mydate|datedate}} it shows "22/03/2016"

Thank you all for your help.

Upvotes: 1

Clément Mangin
Clément Mangin

Reputation: 434

If I understand correctly, you want a localized custom date format. Django's documentation has something for you: Creating custom format files

Upvotes: 1

Related Questions