Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current locale's "short_date_format" on django?

I have L10N successfuly set up on my project, and I'm able to translate dates to the correct format. But now I need the SHORT_DATE_FORMAT for the current locale on my templates, or in a context_processor.

Getting it from django.conf.settings always gives me the default value, m/d/Y. The locale was currently set to pt_BR, so the format should be d/m/Y.

In [42]: settings.LANGUAGE_CODE
Out[42]: 'pt-br'

In [43]: settings.USE_L10N
Out[43]: True

In [44]: settings.SHORT_DATE_FORMAT
Out[44]: 'm/d/Y'

Any clues?

By the way, what I'm really trying to do is: get the current locale's format so I can pass it to bootstrap-datepicker plugin. It's currently using m/d/Y and django is giving a date in the d/m/Y format.

like image 973
Marcio Cruz Avatar asked Apr 25 '13 03:04

Marcio Cruz


People also ask

What is locale date format?

Java DateFormat The locale is used for specifying the region and language for making the code more locale to the user. The way of writing date is different in different regions of the world.

What is date localization?

Date and time localization is quite an important topic when it comes to developing applications and software systems in multiple languages. Date and time are written and read in different ways in different countries.

What is en US date format?

The United States is one of the few countries that use “mm-dd-yyyy” as their date format–which is very very unique! The day is written first and the year last in most countries (dd-mm-yyyy) and some nations, such as Iran, Korea, and China, write the year first and the day last (yyyy-mm-dd).

What is locale time?

Locale determines the human language and cultural norms used when generating a String to represent a date-time value. The time zone determines the wall-clock time of a particular region used to represent a moment on the timeline.


1 Answers

I had to do the following:

from django.conf import settings
from django.utils import formats 

correct_format = formats.get_format("SHORT_DATE_FORMAT", lang=settings.LANGUAGE_CODE)
like image 97
epineda Avatar answered Oct 08 '22 20:10

epineda