在Discord.py中添加任何UNIX时间戳

发布于 2025-02-07 15:17:20 字数 610 浏览 1 评论 0原文

“ https://i.sstatic.net/dbhuf.png”中添加unix timestamp

我想在此

@client.command()
async def giveaway(ctx, days:int, hours:int, minutes:int, seconds:int, prize:str):
  time = days + hours + minutes + seconds
  embed = discord.Embed(title=prize, description=f"time: <t:{time}:R>")
  await ctx.send(embed=embed, delete_after=time)
  endembed = discord.Embed(title=f"you have won {prize}", description="The time has ended")
  await ctx.send(embed=endembed)

image

i want to add unix timestamp in the giveaway time like in this image

here is the code:

@client.command()
async def giveaway(ctx, days:int, hours:int, minutes:int, seconds:int, prize:str):
  time = days + hours + minutes + seconds
  embed = discord.Embed(title=prize, description=f"time: <t:{time}:R>")
  await ctx.send(embed=embed, delete_after=time)
  endembed = discord.Embed(title=f"you have won {prize}", description="The time has ended")
  await ctx.send(embed=endembed)

when I run the code it shows me this image

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤君无依 2025-02-14 15:17:20

您可以在时间模块中使用mktime函数来执行此操作。

import datetime
import time

def convert_to_unix_time(date: datetime.datetime, days: int, hours: int, minutes: int, seconds: int) -> str:
    # Get the end date
    end_date = date + datetime.timedelta(days=days, hours=hours, minutes=minutes, seconds=seconds)

    # Get a tuple of the date attributes
    date_tuple = (end_date.year, end_date.month, end_date.day, end_date.hour, end_date.minute, end_date.second)

    # Convert to unix time
    return f'<t:{int(time.mktime(datetime.datetime(*date_tuple).timetuple()))}:R>'

返回的值应该像这样有点像这样:&lt; t:1655392367:r&gt;

粘贴返回的字符串将提供一个动态显示,如您提供的图像所示。

MKTime函数返回浮点值。为了使日期时间动态显示,两者之间的值必须仅为数字。

返回的字符串末尾的r代表相对。动态显示将相对于当前时间。 其他格式是t,,> tddff

You can do this by using the mktime function in the time module.

import datetime
import time

def convert_to_unix_time(date: datetime.datetime, days: int, hours: int, minutes: int, seconds: int) -> str:
    # Get the end date
    end_date = date + datetime.timedelta(days=days, hours=hours, minutes=minutes, seconds=seconds)

    # Get a tuple of the date attributes
    date_tuple = (end_date.year, end_date.month, end_date.day, end_date.hour, end_date.minute, end_date.second)

    # Convert to unix time
    return f'<t:{int(time.mktime(datetime.datetime(*date_tuple).timetuple()))}:R>'

The returned value should like somewhat like this: <t:1655392367:R>

Pasting the returned string will give a dynamic display as shown in the image you have provided.

The mktime function returns a float value. For the datetime to be displayed dynamically, the value in between must be numbers only.

The R in the end of the returned string stands for relative. The dynamic display will be relative to current time. The other formats are t, T, d, D, f and F.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文