时间戳: 转字符串 转时间元组 字符串 转时间戳 转时间元组 时间元组 转字符串 转时间戳 时间戳转 1import time2from datetime import datetime3 4# 可手动指定5time_stamp = time.time() #1633763846.8836276 7# 转元组8time.gmtime(time_stamp) # UTC 时间9time.localtime(time_stamp)10# 返回 time.struct_time(tm_year=2021, tm_mon=10, tm_mday=11, tm_hour=20, tm_min=4, tm_sec=50, tm_wday=0, tm_yday=284, tm_isdst=0)11datetime.fromtimestamp(time_stamp)12# 返回 datetime.datetime(2021, 10, 11, 20, 4, 3, 315279)13 14# 转指定字符串格式15# strftime 需要先把时间戳转成元组格式16time.strftime('%Y-%m-%d %H:%M%S', time.localtime(time_stamp))17datetime.utcfromtimestamp(time_stamp).strftime('%Y-%m-%d %H:%M:%S')18# 返回 2011-11-11 06:07:42 字符串转 1from datetime import datetime2 3# 转时间元组(date.isoformat() 逆操作)4datetime.fromisoformat('2021-12-12 06:07:42')5# 返回 datetime.datetime(2021, 12, 12, 6, 7, 42) 指定格式转 datetime 1from datetime import time2 3# 转换 20220329_002128 为 datetime 格式4datetime.strptime('20220329_002128', '%Y%m%d_%H%M%S')5# 返回 datetime.datetime(2022, 3, 29, 0, 21, 28) 时间元组转 1import time2from datetime import datetime3 4# 转时间戳5time.mktime(time.localtime()) # 可自行提供9个参数6 7# 转字符串8time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())9datetime.now().strftime('%Y-%m-%d %H:%M:%S')10# 返回 2021-10-11 23:24:21