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'

猜你喜欢

转载自www.cnblogs.com/calvin-zhang/p/11645641.html