python - 计算两个日期时间之间的差异?
问题描述
from datetime import datetime
import pytz
cur_time = datetime.now()
cur_time = pytz.utc.localize(cur_time)
rr_name = 'xx_20211005141746.txt'
rr_time = re.search('_(.*).txt', rr_name).group(1)
rr_time = datetime.strptime(rr_time, '%Y%m%d%H%M%S')
tzone = "Europe/London"
hb_time = pytz.utc.localize(rr_time).astimezone(pytz.timezone(tzone))
diff = cur_time - hb_time
l_heared = round((diff.total_seconds() / 60), 2)
cur_time
和之间的差rr_time
大约是 6 分钟,也就是 360 秒。delta怎么让我77.65
秒回...
请注意cur_time
,并且rr_name
是本地日期时间
解决方案
使用total_seconds
方法:
print(f'{delta.total_seconds()} seconds')
输出:337.525332 seconds
推荐阅读
- kubernetes - Kubernetes 发布请求的 cpu
- qt - 如何在不使用 Yocto 构建的情况下启用 etnaviv 驱动程序?
- azure-cosmosdb - azure cosmosDB 有 @Query 吗?(文档数据库)
- vuetify.js - 如何更改 V-Data-Table 页脚的默认样式?
- bash - tmux:在窗格中运行命令并在 bash 脚本中捕获结果
- javafx - JavaFX(场景生成器)箭头键 OnKeyPressed
- node.js - 运行“npm install”后,Docker 构建(Windows)挂起
- yii2 - 如何根据 Yii2 中的 radioList 更改显示/隐藏 div 框?
- loops - 使用ffmpeg批量处理文件夹中的所有文件?
- video-streaming - 媒体基金会:英特尔硬件 MFT 的 SPS/PPS 问题