python time库使用

time1 = '2021-05-10 18:44:36'
time2 = '2021-05-10 17:44:36'
time3 = '2021-05-10 17:50:36'

timelist = []
timelist.append(time1)
timelist.append(time2)
timelist.append(time3)

print(sorted(timelist))
print(sorted(timelist,reverse=True))

t=time.mktime(time.strptime(time1,'%Y-%m-%d %H:%M:%S'))
print(type(time1))
print(type(time.strptime(time1,'%Y-%m-%d %H:%M:%S')))
print(type(t))
print(t)

运行结果:

['2021-05-10 17:44:36', '2021-05-10 17:50:36', '2021-05-10 18:44:36']
['2021-05-10 18:44:36', '2021-05-10 17:50:36', '2021-05-10 17:44:36']
<class 'str'>
<class 'time.struct_time'>
<class 'float'>
1620643476.0

原文地址:https://www.cnblogs.com/yangxiayi1987/p/14803655.html