如何更改 django 日期时间格式输出? [英] How to change django datetime format output?

查看:47
本文介绍了如何更改 django 日期时间格式输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期时间对象.这是我的模板文件:

I have a datetime object. This is my template file:

<ul>
<li>{{ sale.validity }}</li>
</ul>

我得到的输出格式如下:

and the output i'm getting is in the format:

2012 年 7 月 18 日,午夜

July 18, 2012, midnight

我真的很想将输出更改为数字格式:day-month-year 所以例如上面的内容将更改为:

I would really like to change the output to be numerical in the format: day-month-year so for example the above would be changed to:

18-07-2012

有人能指出我正确的方向吗?

Can someone point me out to the right direction on this?

推荐答案

Per Django 文档,您可以使用 date 过滤器来格式化日期:

Per Django docs, you can use a date filter to format date:

{{ sale.validity|date:"d-m-Y"}} 

此外,您还可以设置 DATE_FORMAT(或DATETIME_FORMAT) 设置以设置项目范围的默认值,如果 USE_L10N 为 False,则显示此类值.如果这是真的,你会想看看这个关于格式本地化的文档.

Additionally, you can also set DATE_FORMAT (or DATETIME_FORMAT) setting to set a project-wide default for displaying such values if USE_L10N is False. If it's true, you'll want to have a look at this documentation on format localization.

这篇关于如何更改 django 日期时间格式输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆