python3获取各种时间

>>> import datetime

>>> from datetime import timedelta

>>> now=datetime.datetime.now()
>>> today=now

#今天
>>> now
datetime.datetime(2019, 10, 9, 17, 25, 9, 459542)

#格式化今天
>>> now.strftime('%Y-%m-%d %H:%M:%S')
'2019-10-09 17:25:09'

#昨天

>>> yesterday=now-timedelta(days=1)
>>> yesterday
datetime.datetime(2019, 10, 8, 17, 25, 9, 459542)

#格式化昨天

>>> yesterday.strftime('%Y-%m-%d %H:%M:%S')
'2019-10-08 17:25:09'

 

posted @ 2019-10-10 08:28  Calvin_zhang  阅读(593)  评论(0编辑  收藏  举报