时间的操作

将元组时间转换为字符串格式时间
import time
>>> x = time.localtime()
>>> time.strftime('%Y-%m-%d %H:%M:%S',x)
'2018-02-09 19:26:42'



import datetime
1.返回当前时间
>>> datetime.datetime.now()
datetime.datetime(2017, 5, 9, 17, 7, 0, 514481)

2.时间戳转换成日期
>>> datetime.date.fromtimestamp(1178766678)
datetime.date(2007, 5, 10)

3.当前时间+3天
>>> datetime.datetime.now() + datetime.timedelta(+3)
datetime.datetime(2017, 5, 12, 17, 12, 42, 124379)

4.当前时间-3天
>>> datetime.datetime.now() + datetime.timedelta(-3)
datetime.datetime(2017, 5, 6, 17, 13, 18, 474406)

5.当前时间+3小时
>>> datetime.datetime.now() + datetime.timedelta(hours=3)
datetime.datetime(2017, 5, 9, 20, 13, 55, 678310)

6.当前时间+30分钟
>>> datetime.datetime.now() + datetime.timedelta(minutes=30)
datetime.datetime(2017, 5, 9, 17, 44, 40, 392370)
 

 

posted @ 2019-08-06 22:50  不不田鼠  阅读(128)  评论(0编辑  收藏  举报