Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Localized date strftime in Django view

I would like to send localized date in JSON from django view

Normal text translation via

ugettext

is OK

Following code in view has no effect:

translation.activate("ru") print datetime.now().strtime("%B") 

Output is "August", instead of "Август"

I read about python's "locale" module, but it's named as thread unsafe

How to force strftime to use django's locale?

like image 753
Andrew Avatar asked Aug 04 '11 16:08

Andrew


People also ask

How do I get the current date in dd mm yyyy format in Python?

Use strftime() function of a datetime class The format codes are standard directives for mentioning in which format you want to represent datetime. For example, the %d-%m-%Y %H:%M:%S codes convert date to dd-mm-yyyy hh:mm:ss format.

How do I change the date format in Django?

You can use the date_format as follows. from django. utils import formats is the same? docs.djangoproject.com/en/dev/releases/1.2/…

What is Django datetime format?

DateTimeField in Django Forms is a date field, for taking input of date and time from user. The default widget for this input is DateTimeInput. It Normalizes to: A Python datetime. datetime object.

What is USE_L10N in Django?

The formatting system is disabled by default. To enable it, it's necessary to set USE_L10N = True in your settings file. Note. To enable number formatting with thousand separators, it is necessary to set USE_THOUSAND_SEPARATOR = True in your settings file.


1 Answers

Finally i used date filter from django templates:

from django.template.defaultfilters import date as _date from datetime import datetime  _date(datetime.now(), "d b, D") 
like image 115
Andrew Avatar answered Sep 19 '22 21:09

Andrew