149

I have dates in ISO 8601 format in the database, %Y-%m-%d. However, when the date is passed on to the template, it comes out as something like Oct. 16, 2011.

Is there a way that I can manipulate the format to whatever I want?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
bash-
  • 6,144
  • 10
  • 43
  • 51

12 Answers12

371

Within your template, you can use Django's date filter. E.g.:

<p>Birthday: {{ birthday|date:"M d, Y" }}</p>

Gives:

Birthday: Jan 29, 1983

More formatting examples in the date filter docs.

medmunds
  • 5,950
  • 3
  • 28
  • 51
  • 1
    I think this works only if date is provideded as a datetime object. What if it is just a string passed from the view? – Mohammed Shareef C Dec 13 '16 at 05:33
  • 3
    For an arbitrary string containing a date/time, I'd probably parse it into a python datetime in the view code. (Or even earlier if possible -- wherever that date string first arrives in my code.) But if you really want to use strings to pass datetimes to your Django templates, [this answer](http://stackoverflow.com/a/33188293/647002) looks helpful. – medmunds Dec 13 '16 at 17:33
  • Perfect above example to use in the template file. – Vinod Patidar Jan 03 '19 at 09:31
61

date template tag

settings.DATE_FORMAT

Flimm
  • 136,138
  • 45
  • 251
  • 267
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
39

Set both DATE_FORMAT and USE_L10N

To make changes for the entire site in Django 1.4.1 add:

DATE_FORMAT = "Y-m-d"

to your settings.py file and edit:

USE_L10N = False

since l10n overrides DATE_FORMAT

This is documented at: https://docs.djangoproject.com/en/dev/ref/settings/#date-format

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
35

Just use this:

{{you_date_field|date:'Y-m-d'}}

This will show something like 2016-10-16. You can use the format as you want.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
hardcoderrsl
  • 371
  • 3
  • 5
13

You need a date template filter.

E.g:

<td>Joined {{user.date_created|date:"F Y" }}<td>

This returns Joined December 2018.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Toluwalemi
  • 391
  • 5
  • 16
12

If you need to show a short date and time (11/08/2018 03:23 a.m.), you can do it like this:

{{your_date_field|date:"SHORT_DATE_FORMAT"}} {{your_date_field|time:"h:i a"}}

Details for this tag is here and more about dates according to the given format is here.

Example:

<small class="text-muted">Last updated: {{your_date_field|date:"SHORT_DATE_FORMAT"}} {{your_date_field|time:"h:i a"}}</small>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nilsoviani
  • 344
  • 5
  • 16
10

In order to change the date format in the views.py file and then assign it to template:

# get the object details 
home = Home.objects.get(home_id=homeid)

# get the start date
_startDate = home.home_startdate.strftime('%m/%d/%Y')

# assign it to template 
return render_to_response('showme.html', {'home_startdate':_startDate},  context_instance=RequestContext(request) )  
Community
  • 1
  • 1
kta
  • 19,412
  • 7
  • 65
  • 47
5
{{yourDate|date:'*Prefered Format*'}}

Example:

{{yourDate|date:'F d, Y'}}

For the preferred format: Built-in template tags and filters, date

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1

If you are getting an error in calling a Date value in a Django template, try this

{{ViewfieldValue.CreateModelValue|date:"M d, Y" }}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1
{{your_date|date:'Y-m-d'}}

This will show something like 2022-12-02. You can use the format as you want.

José Castro
  • 3,976
  • 2
  • 22
  • 26
Ajeesh K H
  • 11
  • 2
1
{% load temp_tags %}

     <div class="d-flex ">
                    <input type="text" class="form-control me-2 w-25 show_error"  name="lender_dob_day" aria-describedby=""
                        {% if lender_dob %} value="{{ lender_dob|split:'-'|last }}" {% else %} value="" {% endif %} placeholder="DD">
                    <input type="text" class="form-control me-2 w-25 show_error"  name="lender_dob_month" aria-describedby=""
                        {% if lender_dob %} value="{{ lender_dob|split:'-'|slice:'1:2'|first }}" {% else %} value="" {% endif %} placeholder="MM">
                    <input type="text" class="form-control flex-1 show_error" name="lender_dob_year" aria-describedby=""
                        {% if lender_dob %} value="{{ lender_dob|split:'-'|first }}" {% else %} value="" {% endif %} placeholder="YYYY">
                    </div>






@register.filter
def split(value, delimiter):
    return value.split(delimiter)
rambo_gt_r
  • 11
  • 2
0

Code:

{{event.event_date|date:"M d, Y"}}

The output will be like this: Aug 17, 2022

MD. SHIFULLAH
  • 913
  • 10
  • 16