import datetime nowTime = datetime.datetime.now().strftime(‘%Y-%m-%d %H:%M:%S‘)#现在 pastTimeMinutes = (datetime.datetime.now()+datetime.timedelta(minutes=5)).strftime(‘%Y-%m-%d %H:%M:%S‘)#5分钟后 pastTime = (datetime.datetime.now()-datetime.timedelta(hours=1)).strftime(‘%Y-%m-%d %H:%M:%S‘)#一小时前 afterTomorrowTime = (datetime.datetime.now()+datetime.timedelta(days=2)).strftime(‘%Y-%m-%d %H:%M:%S‘)#后天 yesterday = (datetime.datetime.now()-datetime.timedelta(days=1)).strftime(‘%Y-%m-%d %H:%M:%S‘)#昨天 tomorrowTime = (datetime.datetime.now()+datetime.timedelta(days=1)).strftime(‘%Y-%m-%d %H:%M:%S‘)#明天 # print(‘\n‘,nowTime,‘\n‘,pastTimeMinutes,‘\n‘,pastTime,‘\n‘,afterTomorrowTime,‘\n‘,tomorrowTime) print("现在: "+nowTime) print("5分钟后: "+pastTimeMinutes) print("一小时前: "+pastTime) print("后天: "+afterTomorrowTime) print("昨天: "+yesterday) print("明天: "+tomorrowTime)
import datetime nowTime = datetime.datetime.now().strftime(‘%Y-%m-%d %H:%M:%S‘)#现在 pastTimeMinutes = (datetime.datetime.now()+datetime.timedelta(minutes=5)).strftime(‘%Y-%m-%d %H:%M:%S‘)#5分钟后 pastTime = (datetime.datetime.now()-datetime.timedelta(hours=1)).strftime(‘%Y-%m-%d %H:%M:%S‘)#一小时前 afterTomorrowTime = (datetime.datetime.now()+datetime.timedelta(days=2)).strftime(‘%Y-%m-%d %H:%M:%S‘)#后天 yesterday = (datetime.datetime.now()-datetime.timedelta(days=1)).strftime(‘%Y-%m-%d %H:%M:%S‘)#昨天 tomorrowTime = (datetime.datetime.now()+datetime.timedelta(days=1)).strftime(‘%Y-%m-%d %H:%M:%S‘)#明天 # print(‘\n‘,nowTime,‘\n‘,pastTimeMinutes,‘\n‘,pastTime,‘\n‘,afterTomorrowTime,‘\n‘,tomorrowTime) print("现在: "+nowTime) print("5分钟后: "+pastTimeMinutes) print("一小时前: "+pastTime) print("后天: "+afterTomorrowTime) print("昨天: "+yesterday) print("明天: "+tomorrowTime)
总结:
+datetime.timedelta(minutes=5) 加5分钟也就是5分钟之后的时间
-datetime.timedelta(days=1) 减1天时间也就是1天前的时间
原文地址:https://www.cnblogs.com/111testing/p/10424933.html
时间: 2024-10-18 23:21:09