python - datetime日期加减

import datetime
now = datetime.datetime.now()
# now = datetime.datetime(2023,4,9)

now.strftime('%Y-%m-%d %H:%M:%S')
# 2023-04-09 21:00:41

# 加7天
delta7 = datetime.timedelta(days=7)
(now+delta7).strftime('%Y-%m-%d %H:%M:%S')
# 2023-04-16 21:00:41

# 减7天
delta7 = datetime.timedelta(days=7)
(now-delta7).strftime('%Y-%m-%d %H:%M:%S')
# 2023-04-02 21:00:41
posted @ 2023-04-09 21:10  wstong  阅读(161)  评论(0编辑  收藏  举报