Python中datetime用法

Python中一些datetime的用法

from datetime import datetime, date, timedelta
import time

# 昨日日期
a = date.today() - timedelta(days=1)
print(a)
# 当日日期
b = datetime.now().strftime('%Y-%m-%d')
print(b)
# 当日星期
c = time.strftime("%w")
print(c)
# 上月月数
d = (date.today().replace(day=1) - timedelta(days=1)).strftime("%m")
print(d)
# 本月月数
e = datetime.now().month
print(e)
# 获取上星期日到上星期一的日期
now = datetime.now()
last_week_start = (now - timedelta(days=now.weekday()+7)).strftime('%Y-%m-%d')
last_week_end = (now - timedelta(days=now.weekday()+1)).strftime('%Y-%m-%d')
print(last_week_start,last_week_end)

你可能感兴趣的:(Python中datetime用法)