将Django DateField格式化为ISO格式

时间:2022-10-04 19:27:50

I'm using Django to serve up some XML. Part of the requirements is that my dates are formatted as follows: 1969-12-31T18:33:28-06:00

我正在使用Django来提供一些XML。部分要求是我的日期格式如下:1969-12-31T18:33:28-06:00

How can I output a DateField in Django in that format?

如何以该格式在Django中输出DateField?

4 个解决方案

#1


3  

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:

如果您在Django文档中查看日期,您将看到支持日期的各种格式。基本上在您需要的模板中:

{{ 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.

其中格式字符串可以是上面页面上日期定义的格式字符串之一,也可以是现在函数(包括ISO 8601格式)下定义的自定义格式字符串之一,该格式字符串也在上面链接的页面上。我假设当你输出到xml时,你的方式与普通网页的模板类似。

Update (August 2014)

更新(2014年8月)

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

如Edgar R的另一个答案所示,从版本1.2开始,您现在可以使用内置开关使用格式字符串输出ISO 8601格式的日期:c

#2


20  

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

Django 1.2中的新功能,有一个模板标签可以输出您想要的内容:

{{ value|date:"c"}}

From the Django template documentation:

从Django模板文档:

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

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

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

从技术上讲,这更接近W3-DTF,但足够接近

#3


6  

Since Django is written in Python,

由于Django是用Python编写的,

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

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

datetime(2002, 12, 25, tzinfo=TZ()).isoformat()

#4


1  

Check out these docs:

看看这些文档:

http://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#date http://docs.djangoproject.com/en/dev/ref/settings/#setting-DATE_FORMAT http://docs.djangoproject.com/en/dev/ref/templates/builtins/#ttag-now

http://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#date http://docs.djangoproject.com/en/dev/ref/settings/#setting-DATE_FORMAT http: //docs.djangoproject.com/en/dev/ref/templates/builtins/#ttag-now

This should be enough for you to format your date any way, you wish.

这应该足以让您以任何方式格式化日期,您希望如此。

#1


3  

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:

如果您在Django文档中查看日期,您将看到支持日期的各种格式。基本上在您需要的模板中:

{{ 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.

其中格式字符串可以是上面页面上日期定义的格式字符串之一,也可以是现在函数(包括ISO 8601格式)下定义的自定义格式字符串之一,该格式字符串也在上面链接的页面上。我假设当你输出到xml时,你的方式与普通网页的模板类似。

Update (August 2014)

更新(2014年8月)

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

如Edgar R的另一个答案所示,从版本1.2开始,您现在可以使用内置开关使用格式字符串输出ISO 8601格式的日期:c

#2


20  

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

Django 1.2中的新功能,有一个模板标签可以输出您想要的内容:

{{ value|date:"c"}}

From the Django template documentation:

从Django模板文档:

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

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

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

从技术上讲,这更接近W3-DTF,但足够接近

#3


6  

Since Django is written in Python,

由于Django是用Python编写的,

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

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

datetime(2002, 12, 25, tzinfo=TZ()).isoformat()

#4


1  

Check out these docs:

看看这些文档:

http://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#date http://docs.djangoproject.com/en/dev/ref/settings/#setting-DATE_FORMAT http://docs.djangoproject.com/en/dev/ref/templates/builtins/#ttag-now

http://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#date http://docs.djangoproject.com/en/dev/ref/settings/#setting-DATE_FORMAT http: //docs.djangoproject.com/en/dev/ref/templates/builtins/#ttag-now

This should be enough for you to format your date any way, you wish.

这应该足以让您以任何方式格式化日期,您希望如此。