具有“st”,“nd”,“rd”,“th”的python格式datetime (英文序数后缀),如PHP的“S” [英] python format datetime with "st", "nd", "rd", "th" (english ordinal suffix) like PHP's "S"

查看:416
本文介绍了具有“st”,“nd”,“rd”,“th”的python格式datetime (英文序数后缀),如PHP的“S”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个python datetime对象输出(并使用django中的结果),如下所示:

I would like a python datetime object to output (and use the result in django) like this:

Thu the 2nd at 4:30

但是我发现python没有办法输出 st nd rd th 像我可以用PHP日期时间格式与 S string(他们称之为英文Ordinal后缀)( http://uk.php.net/manual/en/function.date.php )。

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

在django / python中是否有内置的方法? strftime 不够好 http://docs.python.org/library/datetime.html#strftime --strptime-behavior )。

Django有一个过滤器,可以做我想要的,但是我想要一个功能,而不是一个过滤器来做我想要的。一个django或者python函数会很好。

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.

推荐答案

django.utils.dateformat 有一个函数格式需要两个参数,第一个是日期(一个 datetime.date [[或 datetime.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 (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. 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.

这篇关于具有“st”,“nd”,“rd”,“th”的python格式datetime (英文序数后缀),如PHP的“S”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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