时间操作
时间操作
把当前时间转为特定时间字符串
time_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
结果输出:
'2023-05-18 09:43:39'
将时间字符串转为时间戳
timeStr = '20221026221000'
timeArray = time.strptime(timeStr, '%Y%m%d%H%M%S')
timeStamp = int(time.mktime(timeArray))
print(timeStamp)
结果输出:
1666793400
时间戳转为时间
timeStamp = 1666793400
timeArray = time.localtime(timeStamp)
otherStyleTime = time.strftime("%Y/%m/%d %H:%M:%S", timeArray)
结果输出:
'2022/10/26 22:10:00'
当前时间是'2022/11/01 01:30:10'
,计算过了11.39s后的时间
time_stamp = time.mktime(time.strptime('2022/11/01 01:30:10', '%Y/%m/%d %H:%M:%S'))
time_end = time_stamp + 11.39
timeArray = time.localtime(time_end)
otherStyleTime = time.strftime("%Y/%m/%d %H:%M:%S", timeArray)
结果输出:
'2022/11/01 01:30:21'