例子
time模块:
import time
t1 = time.time()#返回当前时间
t2 = time.localtime()#返回本地时间以元组的形式表示
t3 = time.asctime()#返回时间格式"Tue Mar 19 18:14:33 2019"
t4 = time.gmtime()#返回UTC(国际标准时间) 以元组的形式表示
t5 = time.altzone#返回与utc时间的时间差,以秒计算
t6 = time.strftime("%Y-%m-%d %H:%M:%S",t2)#元组格式转化成字符串格式
t7 = time.strptime(t6,"%Y-%m-%d %H:%M:%S")#字符串格式转化成元组
t8 = time.mktime(t2)#元组转化成时间戳
t9 = time.gmtime(t1)#时间戳转化成元组
print("t1:",t1)
print("t2:",t2)
print("t3:",t3)
print("t4:",t4)
print("t5:",t5)
print("t6:",t6)
print("t7:",t7)
print("t8:",t8)
print("t9:",t9)
t1: 1552990473.2945867
t2: time.struct_time(tm_year=2019, tm_mon=3, tm_mday=19, tm_hour=18, tm_min=14, tm_sec=33, tm_wday=1, tm_yday=78, tm_isdst=0)
t3: Tue Mar 19 18:14:33 2019
t4: time.struct_time(tm_year=2019, tm_mon=3, tm_mday=19, tm_hour=10, tm_min=14, tm_sec=33, tm_wday=1, tm_yday=78, tm_isdst=0)
t5: -32400
t6: 2019-03-19 18:14:33
t7: time.struct_time(tm_year=2019, tm_mon=3, tm_mday=19, tm_hour=18, tm_min=14, tm_sec=33, tm_wday=1, tm_yday=78, tm_isdst=-1)
t8: 1552990473.0
t9: time.struct_time(tm_year=2019, tm_mon=3, tm_mday=19, tm_hour=10, tm_min=14, tm_sec=33, tm_wday=1, tm_yday=78, tm_isdst=0)
datatime模块
import datetime
t1 = datetime.datetime.now() #返回本地时间2018-10-08 20:48:30.804745
t2 = datetime.datetime.strftime(t1,'%Y-%m-%d')#时间戳转化成日期
t3 = t1+datetime.timedelta(days=2)#加两天,days=-2表示减两天,hours=3表示加3小时,hours=-3表示减3小时
print("t1:",t1)
print("t2:",t2)
print("t3:",t3)
t1: 2019-03-19 18:23:28.247201
t2: 2019-03-19
t3: 2019-03-21 18:23:28.247201