2022年 11月 4日

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17