如何使用 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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP