简体   繁体   English

python 格式日期时间,带有“st”、“nd”、“rd”、“th”(英文序数后缀),如 PHP 的“S”

[英]python format datetime with “st”, “nd”, “rd”, “th” (english ordinal suffix) like PHP's “S”

I would like a python datetime object to output (and use the result in django) like this:我想要一个 python datetime 对象像这样输出(并在 django 中使用结果):

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 ).但是我发现在 python 中没有办法输出stndrdth就像我可以使用带有S字符串的 PHP 日期时间格式(他们称之为“英文序数后缀”)( http://uk.php.net/manual /en/function.date.php )。

Is there a built-in way to do this in django/python?在 django/python 中是否有内置的方法来做到这一点? strftime isn't good enough ( http://docs.python.org/library/datetime.html#strftime-strptime-behavior ). strftime不够好( 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. Django 有一个过滤器可以做我想要的,但我想要一个函数而不是过滤器来做我想做的。 Either a django or python function will be fine. django 或 python 函数都可以。

The django.utils.dateformat has a function format that takes two arguments, the first one being the date (a datetime.date [[or datetime.datetime ]] instance, where datetime is the module in Python's standard library), the second one being the format string, and returns the resulting formatted string. django.utils.dateformat有一个函数format ,它接受两个参数,第一个是日期(一个datetime.date [[或datetime.datetime ]] 实例,其中datetime是 Python 标准库中的模块),第二个是作为格式字符串,并返回生成的格式化字符串。 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.大写的S格式项(当然,如果是格式字符串的一部分)是根据日期扩展为 'st'、'nd'、'rd' 或 'th' 中的正确项的项相关日期的月份。

dont know about built in but I used this...不知道内置,但我用过这个......

def ord(n):
    return str(n)+("th" if 4<=n%100<=20 else {1:"st",2:"nd",3:"rd"}.get(n%10, "th"))

and:和:

def dtStylish(dt,f):
    return dt.strftime(f).replace("{th}", ord(dt.day))

dtStylish can be called as follows to get Thu the 2nd at 4:30 . dtStylish 可以按如下方式调用以获得Thu the 2nd at 4:30 Use {th} where you want to put the day of the month ("%d" python format code )使用{th}放置月份中的日期(“%d” python 格式代码

dtStylish(datetime(2019, 5, 2, 16, 30), '%a the {th} at %I:%M')

You can do this simply by using the humanize library您只需使用humanize 库即可完成此操作

from django.contrib.humanize.templatetags.humanize import ordinal

You can then just give ordinal any integer, ie然后你可以只给出序数任何整数,即

ordinal(2) will return 2nd ordinal(2)将返回2nd

I've just written a small function to solve the same problem within my own code:我刚刚编写了一个小函数来解决我自己的代码中的相同问题:

def foo(myDate):
    date_suffix = ["th", "st", "nd", "rd"]

    if myDate % 10 in [1, 2, 3] and myDate not in [11, 12, 13]:
        return date_suffix[myDate % 10]
    else:
        return date_suffix[0]

Here is the fancier code :这是更高级的代码:

def date_extention(number):
    if number%10 == 1:
        return '%dst' % number
    if number%10 == 2:
        return '%dnd' % number
    if number%10 == 3:
        return '%drd' % number
    if (number%10 >= 4) or (number%10== 0):
        return '%dth' % number

By the way this works with any number so i recommend you make a module out of it.顺便说一下,这适用于任何数字,所以我建议你用它制作一个模块。 Especially if you make a lot of user inputed programs...特别是如果你制作了很多用户输入的程序......

Following solution I got for above problem:以下解决方案我得到了上述问题:

datetime.strptime(mydate, '%dnd %B %Y')
datetime.strptime(mydate, '%dst %B %Y')
datetime.strptime(mydate, '%dth %B %Y')
datetime.strptime(mydate, '%drd %B %Y')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM