时间计算器
import datetime
'''
计算当前时间的前后几天,或后前几小时,或后前几分钟等的时间,支持天时分秒组合,只需要传对应的值
:param days
:param hours
:param minutes
:param seconds
:param type True:往后计算,即+; False:往前计算,即-,默认计算+
'''
def tiem_(days=0, hours=0, minutes=0, seconds=0, type=True):
time = datetime.datetime.now()
if type:
res_time = time + datetime.timedelta(days=days, hours=hours, minutes=minutes,
seconds=seconds)
else:
res_time = time - datetime.timedelta(days=days, hours=hours, minutes=minutes,
seconds=seconds)
return str(res_time)
print(datetime.datetime.now())
print(tiem_(days=2))
print(tiem_(days=2, type=False))
'''
结果:
2022-11-14 16:36:44.846067
2022-11-16 16:36:44.846067
2022-11-12 16:36:44.846067
'''
标签:seconds,计算器,days,datetime,hours,时间,time,minutes
From: https://www.cnblogs.com/chunyouqudongwuyuan/p/16889473.html