import datetime # 获取当天最小时间 print(datetime.datetime.combine(datetime.date.today(),datetime.time.min)) print(datetime.datetime(2021, 11, 16, 0, 0)) # 获取当天最大时间 print(datetime.datetime.combine(datetime.date.today(),datetime.time.max)) print(datetime.datetime(2021, 11, 16, 23, 59, 59, 999999))
运行结果:
2 获取当前日期的前几天/后几天# 获取明天 print(datetime.date.today() + datetime.timedelta(days=1)) # 获 取 前 一 天 print(datetime.date.today() - datetime.timedelta(days=1))
运行结果:
3 获取本周或本月第一天及最后一天#获取本周第一天及最后一天 d_today = datetime.date.today() print(d_today) # 获取本周第一天 print(d_today - datetime.timedelta(d_today.weekday())) # 获取本周最后一天 print(d_today + datetime.timedelta(6-d_today.weekday()))
运行结果:
4 计算两个日期相差多少天# timedelta对象的计算 dt2 = d_today + datetime.timedelta(6-d_today.weekday()) td1 = dt2 - d_today print(td1) #相差多少秒 print(td1.total_seconds())
运行结果:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)