import time,datetime # 当前时间转时间戳 maintenance_time = (datetime.datetime.now() + datetime.timedelta()).strftime("%Y-%m-%d %H:%M:%S") struct_time = time.strptime(maintenance_time,'%Y-%m-%d %H:%M:%S') second_timestamp = int(time.mktime(struct_time)) # 秒级别 print(second_timestamp) millisecond_timestamp = int(time.mktime(struct_time)) * 1000 # 毫秒级别 print(millisecond_timestamp) 结果: 1666961321 1666961321000 # 时间戳转时间字符串 time_stamp = millisecond_timestamp / 1000 # 毫秒级别 time_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time_stamp)) print(time_str) time_stamp = second_timestamp # 秒级别 time_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time_stamp)) print(time_str) 结果: 2022-10-28 20:48:41 2022-10-28 20:48:41
标签:-%,转换,python,timestamp,datetime,stamp,时间,time,print From: https://www.cnblogs.com/lucktomato/p/16837463.html