1、字符类型的时间 转为 时间数组
t1 = '2013-10-10 23:40:00'
timeArray = time.strptime(t1, "%Y-%m-%d %H:%M:%S")
# 结果如下
time.struct_time(tm_year=2013, tm_mon=10, tm_mday=10, tm_hour=23, tm_min=40, tm_sec=0, tm_wday=3, tm_yday=283, tm_isdst=-1)
2、字符类型的时间 转为其它显示格式
t2 = "2013-10-10 23:40:00"
# 转为数组
timeArray = time.strptime(t2, "%Y-%m-%d %H:%M:%S")
# 转为其它显示格式
otherStyleTime = time.strftime("%Y/%m/%d %H:%M:%S", timeArray)
print otherStyleTime # 2013/10/10 23:40:00
3、获取当前时间戳
# time获取当前时间戳
now = int(time.time())
timeArray = time.localtime(now)
转换成需要的格式
otherStyleTime = time.strftime("%Y--%m--%d %H:%M:%S", timeArray)
4、datetime获取当前时间,数组格式
now = datetime.datetime.now()
print now
otherStyleTime = now.strftime("%Y--%m--%d %H:%M:%S")
1 def getlogname(): 2 logfilename = time.strftime('%Y%m%d%H%M%S',time.localtime(time.time())) 3 return logfilename 4 5 def getPresentTime(): 6 now = datetime.datetime.now() 7 #print(now) 8 return now