python - 如何计算discord.py中两个时间戳_a和_b之间的差异?
问题描述
如何获得 _a 和 _b 之间的区别?比如小时、分钟和秒的差异
@client.command()
async def timestamp(ctx, id : int,id2 : int):
a = discord.utils.snowflake_time(id)
b = discord.utils.snowflake_time(id2)
_a = "{:02d} : {:02d} : {:02d}".format(a.hour, a.minute, a.second)
_b = "{:02d} : {:02d} : {:02d}".format(b.hour,b.minute,b.second)
解决方案
a
和都是b
对象datetime.datetime
,因此我们可以直接找到时间增量
import datetime
delta = abs(a - b) #always get the positive delta
delta = datetime.timedelta(seconds=int(delta.total_seconds())) #remove microseconds
#remove the above line if you want milliseconds
print(delta) #or str(delta)
hh:mm:ss
如果差异小于一天,这将打印格式的增量。
推荐阅读
- apache-spark - 仅重建 Zeppelin 的 webapp 元素
- prometheus - Grafana 中 Prometheus 数据的堆积图
- reactjs - MongoDb 中的 $set 和 $pull 在同一文档上
- python - Postgres 或 Python 脚本在尝试访问数据库时引发错误
- python - 在 Python 中将两个 json 键值组合为 1 项
- reactjs - 打字稿错误“0”不可分配给类型 QueryKey
- php - 如何在 Elementor 中创建 CSS 过渡/入口
- angular - 带有文件上传更改事件的 Angular 单元测试
- html - 如何在行的底部对齐内容
- c# - 使用 Google 工作区委派服务帐户 - 找不到实体