python获取当前,昨天,明天时间
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python获取当前,昨天,明天时间相关的知识,希望对你有一定的参考价值。
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天前的时间
以上是关于python获取当前,昨天,明天时间的主要内容,如果未能解决你的问题,请参考以下文章