python获取日期

python获取日期

from datetime import datetime
from datetime import timedelta

curr_time=datetime.now() # 现在的时间
today=(curr_time.strftime("%Y-%m-%d"))# 调用strftime方法就是对时间进行格式化
yesterday=(
    (curr_time - timedelta(days=1)).strftime("%Y-%m-%d")
)
tomorrow=(curr_time + timedelta(days=1)).strftime("%Y-%m-%d")
next_week=(
    (curr_time + timedelta(days=7)).strftime("%Y-%m-%d")
)

print("今天日期:",today)
print("昨天日期:",yesterday)
print("明天日期:",tomorrow)
print("下周日期:",next_week)

你可能感兴趣的:(python,开发语言)