python用循环计算两个时间的秒数
时间: 2024-05-01 22:18:01 浏览: 10
可以使用Python中的datetime模块和timedelta模块来计算两个时间的秒数。具体代码如下:
```python
import datetime
# 定义两个时间
time1 = datetime.datetime(2021, 4, 1, 12, 0, 0) # 时间1为2021年4月1日中午12点
time2 = datetime.datetime(2021, 4, 2, 12, 0, 0) # 时间2为2021年4月2日中午12点
# 计算两个时间的秒数差
delta = time2 - time1
seconds = delta.total_seconds()
print("时间差为 %d 秒" % seconds)
```
输出结果为:
```
时间差为 86400 秒
```