如何使用 Python 的 strftime 显示日期,比如 “8 月 5 日”
无法使用 strftime 函数获取诸如 st、nd、rd 和 th 之类的后缀。strftime 函数没有支持这种格式化的指令。你可以创建自己的函数找出后缀,并将其添加到提供的格式化字符串中。
示例
from datetime import datetime
now = datetime.now()
def suffix(day):
suffix = ""
if 4 <= day <= 20 or 24 <= day <= 30:
suffix = "th"
else:
suffix = ["st", "nd", "rd"][day % 10 - 1]
return suffix
my_date = now.strftime("%b %d" + suffix(now.day))
print(my_date)输出
以下为输出:
Jan 15th
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP