1 timeStamp = time.time() #获取当前的时间戳 2 print(timeStamp) 3 # 1555555453.6283455 4 5 timeTuple = time.localtime(timeStamp) #时间戳 --> 时间元组 6 print(timeTuple) 7 # time.struct_time(tm_year=2019, tm_mon=4, tm_mday=18, tm_hour=10, tm_min=46, tm_sec=25, tm_wday=3, tm_yday=108, tm_isdst=0) 8 9 timeStr = time.strftime('%Y-%m-%d %H:%M:%S', timeTuple) #时间元组 --> 字符串10 print(timeStr)11 # 2019-04-18 10:48:3312 13 new_timeTuple = time.strptime(timeStr, '%Y-%m-%d %H:%M:%S') #字符串 --> 时间元组14 print(new_timeTuple)15 # time.struct_time(tm_year=2019, tm_mon=4, tm_mday=18, tm_hour=10, tm_min=51, tm_sec=40, tm_wday=3, tm_yday=108, tm_isdst=-1)16 17 new_timeStamp = time.mktime(new_timeTuple) #时间元组 --> 时间戳18 print(new_timeStamp)19 # 1555555984.0
几种时间数据类型的相互转换:
- 字符串
- time.strptime
- 时间元组
- time.mktime
- 时间戳
- time.localtime
- 时间元组
- time.strftime
- 字符串