[Django]-Format Django DateField as ISO format

5๐Ÿ‘

โœ…

If you look under date in the Django documentation youโ€™ll see that a variety of formats for the date are supported. Basically in a template you need:

{{ value|date:"some format string" }} 

where the format string can be one of the ones defined under date on the page above or one of the custom ones defined under the now function (which includes ISO 8601 format) which is also on the page linked to above. Iโ€™m assuming that when you output to xml you do so in a similar way to a template for a normal web page.

Update (August 2014)

As indicated in another answer by Edgar R, as of version 1.2 onwards you can now use a built in switch to output the date in ISO 8601 format using format string: c

๐Ÿ‘คAmos

47๐Ÿ‘

Introduced in Django 1.2, there is a template tag that will output what you wish:

{{ value|date:"c"}}

From the Django template documentation:

c | ISO 8601 Format | 2008-01-02T10:30:00.000123

Technically, this closer to W3-DTF, but close enough

๐Ÿ‘คEdgar

13๐Ÿ‘

Since Django is written in Python,

http://docs.python.org/library/datetime.html#datetime.datetime.isoformat

datetime(2002, 12, 25, tzinfo=TZ()).isoformat()
๐Ÿ‘คEmyr

1๐Ÿ‘

๐Ÿ‘คgruszczy

Leave a comment