Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 921
Wilerson Avatar asked Jun 12 '12 00:06

Wilerson


1 Answers

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_DATE_FORMAT', use_l10n=True)
like image 158
jpic Avatar answered Oct 06 '22 18:10

jpic