python 格式日期时间,包含“st”、“nd”、“rd”、“th” (英文序数后缀)如 PHP 的“S”
我想要一个 python datetime 对象来输出(并在 django 中使用结果),如下所示:
Thu the 2nd at 4:30
但我在 python 中找不到输出 st
, nd
, 的方法rd
或 th
就像我可以使用带有 S
字符串的 PHP 日期时间格式(他们称之为“英语序数后缀”)(http://uk.php.net/manual/en/function.date.php)。
django/python 中是否有内置方法可以执行此操作? strftime
还不够好(http://docs.python.org/library/datetime.html#strftime-strptime-behavior)。
Django 有一个过滤器可以执行我想要的操作,但我想要一个函数而不是过滤器来执行我想要的操作。 django 或 python 函数都可以。
I would like a python datetime object to output (and use the result in django) like this:
Thu the 2nd at 4:30
But I find no way in python to output st
, nd
, rd
, or th
like I can with PHP datetime format with the S
string (What they call "English Ordinal Suffix") (http://uk.php.net/manual/en/function.date.php).
Is there a built-in way to do this in django/python? strftime
isn't good enough (http://docs.python.org/library/datetime.html#strftime-strptime-behavior).
Django has a filter which does what I want, but I want a function, not a filter, to do what I want. Either a django or python function will be fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
django.utils.dateformat 有一个函数
format
接受两个参数,第一个是日期(一个datetime.date
[[ordatetime.datetime
]] 实例,其中datetime
是Python标准库中的模块),第二个是格式字符串,并返回结果格式化字符串。大写-S
格式项(当然,如果是格式字符串的一部分)是扩展为 'st'、'nd'、'rd' 或 'th' 中正确的一项,取决于相关日期的具体日期。The django.utils.dateformat has a function
format
that takes two arguments, the first one being the date (adatetime.date
[[ordatetime.datetime
]] instance, wheredatetime
is the module in Python's standard library), the second one being the format string, and returns the resulting formatted string. The uppercase-S
format item (if part of the format string, of course) is the one that expands to the proper one of 'st', 'nd', 'rd' or 'th', depending on the day-of-month of the date in question.不知道内置的,但我用过这个...
并且:
可以按如下方式调用 dtStylish 以获得
Thu the 2nd at 4:30
。在要放置月份日期的位置使用{th}
("%d" python 格式代码)dont know about built in but I used this...
and:
dtStylish can be called as follows to get
Thu the 2nd at 4:30
. Use{th}
where you want to put the day of the month ("%d" python format code)您可以简单地通过使用 humanize 库来完成此操作
from django.contrib. humanize.templatetags. humanize import ordinal
然后您可以只给出 ordinal 任何整数,即
ordinal(2)
将返回第二
You can do this simply by using the humanize library
from django.contrib.humanize.templatetags.humanize import ordinal
You can then just give ordinal any integer, ie
ordinal(2)
will return2nd
我刚刚编写了一个小函数来解决我自己的代码中的相同问题:
I've just written a small function to solve the same problem within my own code:
我自己的版本。使用python的strftime的格式代码,替换 < code>{th} 您想在其中查看序数后缀。
或者
My own version. Use python's strftime's format codes, substituting
{th}
where you want to see the ordinal suffix.or
这是我使用的东西:
Here's something that I use:
实现@Frosty Snomwan 的方法及其扩展
当我第一次尝试实现Frosty Snowman 的出色答案时,我匆忙地误用了它。他是下面的 ord2 函数。我的“不太好的功能”是ord1。他传递了一个数字。我的需要一根绳子。两者都可以轻松编辑以接收不同的输入类型。我将使用他的函数,但我认为某些代码可能会发现这两个函数的完整“实验”实现很有帮助。
输出是
我最终的功能形式,可能更全面有用的是......
Implementing @Frosty Snomwan's Method And An Expansion Of It
When I first tried to implement Frosty Snowman's outstanding answer, I misapplied it in my haste. His is the ord2 function below. My "not as good function" is ord1. He passes in a number. Mine takes a string. Both could be easily edited to receive different input types. I will be using his function, but I thought some codes might find the full "experimental" implementation of both helpful.
Output is
I final functional form that might be more overall useful would be ...
针对上述问题我得到了以下解决方案:
Following solution I got for above problem: