import datetime
from datetime import timedelta
# 获取当前时间
print(datetime.datetime.now())
# 格式化时间
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
# 多加一天
print((datetime.datetime.now()+datetime.timedelta(days=1)).strftime("%Y-%m-%d %H:%M:%S"))
# 多加一小时
print((datetime.datetime.now()+datetime.timedelta(hours=1)).strftime("%Y-%m-%d %H:%M:%S"))
# 多加一分钟
print((datetime.datetime.now()+datetime.timedelta(minutes=1)).strftime("%Y-%m-%d %H:%M:%S"))
# 获取上周日期
today = datetime.datetime.today().date()
next_week_start = today + datetime.timedelta(days=today.weekday() - 7)
next_week_end = today + datetime.timedelta(days=today.weekday() - 1)
print(next_week_start, next_week_end)
# 获取上周时间
now = datetime.datetime.now()
#上周第一天和最后一天
last_week_start = now - timedelta(days=now.weekday()+7)
last_week_end = now - timedelta(days=now.weekday()+1)
print(last_week_start, last_week_end)
python获取日期+时间进行加减
最新推荐文章于 2024-09-12 12:40:24 发布