时间格式处理

root
abc abc
  • 20 Jul

from datetime import datetime

获取当前时间

now = datetime.now()

1. 常用格式:年-月-日 时:分:秒

format1 = now.strftime("%Y-%m-%d %H:%M:%S")
print("格式1:", format1) # 例如:2023-10-05 14:30:25

2. 带星期的格式

format2 = now.strftime("%Y年%m月%d日 %A %H:%M")
print("格式2:", format2) # 例如:2023年10月05日 Thursday 14:30

3. 12小时制(带AM/PM)

format3 = now.strftime("%Y/%m/%d %I:%M:%S %p")
print("格式3:", format3) # 例如:2023/10/05 02:30:25 PM

4. 简洁格式(月缩写+日期+时间)

format4 = now.strftime("%b %d %H:%M")
print("格式4:", format4) # 例如:Oct 05 14:30