time模块使用
>>> import time
>>> time.time()
1524106495.3738947
>>> time.localtime()
time.struct_time(tm_year=2018, tm_mon=4, tm_mday=19, tm_hour=10, tm_min=56, tm_sec=37, tm_wday=3, tm_yday=109, tm_isdst=0)
>>> time.strftime('%Y')
'2018'
>>> time.strftime('%M')
'03'
>>> time.strftime('Y')
'Y'
>>> time.strftime('%Y-%M-%D %H:%M:%S')
'2018-04-04/19/18 14:04:54'
>>> time.strftime('%Y-%M-%D%H:%M:%S')
'2018-05-04/19/1814:05:18'
>>> time.strftime('%Y-%M-%D %H:%M:%S')
'2018-05-04/19/18 14:05:30'
>>> time.strftime('%Y-%m-%d %H:%M:%S')
'2018-04-19 14:09:22'
datetime模块使用
>>> import datetime
>>> datetime.datetime.now() ## 获取当前时间 ## 获取当前时间
datetime.datetime(2018, 4, 19, 15, 53, 16, 764834)
>>> datetime.datetime.now()-datetime.timedelta(5) ## 当前时间减去5天
datetime.datetime(2018, 4, 14, 16, 38, 33, 159454)
>>> datetime.datetime.now()+datetime.timedelta(5) ## 当前时间加上5天 没有定义参数默认为天数
datetime.datetime(2018, 4, 24, 16, 39, 18, 208192)
>>> datetime.datetime.now()+datetime.timedelta(hours=5) ## 当前时间加上5小时
datetime.datetime(2018, 4, 19, 21, 39, 35, 927396)
>>> datetime.datetime.now()+datetime.timedelta(minutes=5) ## 当前时间加上5分钟
datetime.datetime(2018, 4, 19, 16, 53, 9, 28493)
>>>
时间替换
>>> datetime.datetime.now()
datetime.datetime(2018, 4, 19, 20, 19, 2, 533269)
>>> d= datetime.datetime.now()
>>> d.replace(year=2017)
datetime.datetime(2017, 4, 19, 20, 19, 13, 195497)
>>> d.replace(year=2017,month=8)
datetime.datetime(2017, 8, 19, 20, 19, 13, 195497)
>>>