Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 格式日期如:3月1日星期一_Python_Django_Date_Date Formatting - Fatal编程技术网

Python 格式日期如:3月1日星期一

Python 格式日期如:3月1日星期一,python,django,date,date-formatting,Python,Django,Date,Date Formatting,看起来应该很简单,但这让我很恼火。我已经研究了python日期格式字符串,但仍然没有太多意义 下面是我要做的:, 其中是st,nd,rd,th,等等,取决于日期。刚刚找到答案。天知道为什么从每个引用到日期格式都没有链接到它们 {{e.when|date:"l, jS F"}} 模板之外: from django.utils import dateformat dateformat.format(datetime.now(), 'l, jS F') 更新2:看起来OP最终还是觉得这很有用:)

看起来应该很简单,但这让我很恼火。我已经研究了python日期格式字符串,但仍然没有太多意义

下面是我要做的:

其中
st
nd
rd
th
,等等,取决于日期。

刚刚找到答案。天知道为什么从每个引用到日期格式都没有链接到它们

{{e.when|date:"l, jS F"}}
模板之外:

from django.utils import dateformat
dateformat.format(datetime.now(), 'l, jS F')

更新2:看起来OP最终还是觉得这很有用:)

更新:没关系。OP寻找的是Django日期格式,而不是Python

AFAIK没有序号的内置格式说明符。其他的很简单:

my_date.strftime('%A, %d %B')
我在网上发现:

if 4 <= day <= 20 or 24 <= day <= 30:
    suffix = "th"
else:
    suffix = ["st", "nd", "rd"][day % 10 - 1]

if 4相当确定您想要
{{e.when | date:“l,jS F”}
而不是.Aha。Django的日期格式与Python的不同。你介意编辑你的问题,说你正在寻找Django特有的东西吗?这仍然与我的兴趣密切相关。我也会在后台(用Python)处理日期。