burning
burning

Reputation: 2586

Dateformat in Django template

I have a date which I am getting from a REST method like.

2013-04-11 15:03:01.0 I am directly rendering in to the template .

But I want to change the date format .So i have written like this .

 <td>{{x.dateCreated|date:"F j, Y"}}</td>

Unfortunately when I am using then it is not displaying anything .

Please tell me what might I m doing wrong here .

Upvotes: 0

Views: 842

Answers (2)

Tim Selaty Jr.
Tim Selaty Jr.

Reputation: 598

The way Django handles "date" formatting in templates is by parsing a string in a recognized date format. If I'm not mistaken, the string you've passed into the "date" default template tag doesn't appear to be a valid date from the Django default installation. As such, it's unable to parse and output a custom date format.

You can add additional date formats by modifying the "date format" setting. https://docs.djangoproject.com/en/dev/ref/settings/#date-format

Hope that helped!

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1121266

You need to have a datetime instance for that to work. Use datetime.datetime.strptime() to parse your string into such an object:

datetime.datetime.strptime(restdate, '%Y-%m-%d %H:%M:%S.%f')

Upvotes: 1

Related Questions