【问题标题】:How to make this return the value in hours : minute : seconds如何使它以小时:分钟:秒为单位返回值
【发布时间】:2021-08-02 12:33:30
【问题描述】:
它返回时间如下:- 2021-05-12 04:38:03.974000
我应该如何让它只返回hour : minute : second
@client.command()
async def timestamp(ctx, id : int):
a = discord.utils.snowflake_time(id)
await ctx.send(a)
【问题讨论】:
标签:
python
discord
discord.py
bots
【解决方案1】:
discord.utils.snowflake_time(id) 返回一个 Python datetime.datetime 对象,您可以很容易地从中提取小时/分钟/秒:
formatted_a = "{:02d} : {:02d} : {02d}".format(a.hour, a.minute, a.second)
这是使用 {:02d} 而不是 {:d},因此 08:03:05 这样的时间不会被格式化为 8:3:5。
【解决方案2】:
您可以为此使用time.strptime 和time.strftime:
import time
a = '2021-05-12 04:38:03.974000'
formatted_a = time.strftime('%H : %M : %S', time.strptime(a,'%Y-%m-%d %H:%M:%S.%f'))
print(formatted_a)
输出:
04 : 38 : 03