# 打印当前时间的floatnow = time.time() print(now) #1721022981.0060098 单位是s# 浮点数转换成时间格式import datetime# 假设这是你的浮点数时间戳 timestamp_float = 1615128000.0 # 将浮点数转换为整数(秒级) timestamp_int = int(timestamp_float) # 使用datetime.datetime.fromtimestamp将整数时间戳转换为datetime对象 datetime_object = datetime.datetime.fromtimestamp(timestamp_int)# 打印结果 print("浮点数转换成时间格式: ") print(datetime_object) # 2021-03-07 22:40:00# 根据年月日时分秒来计算出对应的浮点数字 然后继续对比from datetime import datetime, timedelta v1 = 1720948767 str1 = datetime.fromtimestamp(v1) # 浮点数转换成时间格式 print(str1) #2024-07-14 17:19:27 r = datetime(2024,7,14,17,37,0) print(r.timestamp()) # 1720949820.0diff = r.timestamp() - v1 print(diff) #1053.0# 获取前一天的日期浮点数字from datetime import datetime, timedelta # 获取今天的日期 today = datetime.now() # 获取前一天的日期 yesterday = today - timedelta(days=1) print(yesterday.now()) # 2024-07-15 15:26:04.013768 print("今天的日期:", today.strftime('%Y-%m-%d %H-%M-%S')) # 今天的日期: 2024-07-15 15-26-04 print("昨天的日期:", yesterday.strftime('%Y-%m-%d')) # 昨天的日期: 2024-07-14