strftime不允许您使用后缀格式化日期。
这是获取正确后缀的方法:
if 4 <= day <= 20 or 24 <= day <= 30: suffix = "th"else: suffix = ["st", "nd", "rd"][day % 10 - 1]
在这里找到
更新:
将基于Jochen的评论的更紧凑的解决方案与gsteff的答案相结合:
from datetime import datetime as dtdef suffix(d): return 'th' if 11<=d<=13 else {1:'st',2:'nd',3:'rd'}.get(d%10, 'th')def custom_strftime(format, t): return t.strftime(format).replace('{S}', str(t.day) + suffix(t.day))print custom_strftime('%B {S}, %Y', dt.now())给出:
May 5th, 2011



