首页 > 解决方案 > 如何计算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)

标签: pythondiscorddiscord.pybots

解决方案


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如果差异小于一天,这将打印格式的增量。


推荐阅读