Wilerson Wilerson - 1 year ago 87
Python Question

How to use a localized "short format" when formatting dates in Django?

I need to display a localized formatted date. If I use django.utils.formats.localize, the date is returned as "June 11, 2012". How could I format the date as to return "06/11/2012", with proper localization (e.g., "11/06/2012 in UK)?

I need something similar to Java's DateFormat.SHORT. Is there something analogous to that?

Answer Source

Yes, there is SHORT_DATE_FORMAT.

In a template, it is possible to use it with the date filter:

{{ your_date_value|date:"SHORT_DATE_FORMAT" }}

Outside of a template, it is possible to use django.utils.formats.date_format, as so:

from django.utils.formats import date_format
date_format(date_obj, format='SHORT_DATETIME_FORMAT', use_l10n=True)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download