Reputation: 5379
I want to convert a Django DateField
to a string according to the DATE_FORMAT
as specified in settings.py
, where I have a certain date format hard-coded. Can it be done without specifying a second (and at best superflous) date format for, e.g., strftime
?
Upvotes: 1
Views: 1477
Reputation: 21807
You can use django.utils.formats.date_format
(Source Code [GitHub]) to do what you want. Note that this is undocumented and appears to be only used by the template tags / templating system and hence may change in future versions.
from django.utils.formats import date_format
# Signature of the function
# date_format(value, format=None, use_l10n=None)
date_string = date_format(some_date_object)
Upvotes: 2