0

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

2
  • 1
    As you mentioned yourself: You need a timestamp of the exact date. You would have to convert it yourself if you want to keep the days, hours... argument(s). Commented Jun 15, 2022 at 12:08
  • Ok, you've done exactly that: you've added a timestamp to an embed. If you can edit your question to include what exactly the problem is, it would help us understand exactly what you're looking for. See How to Ask for more information. Commented Jun 15, 2022 at 13:27

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

i get it now but how can i implement it in my code?
You should be able to simply use end_date.timestamp().
@GameProgrammer, the returned string can be used as the description for the embed. embed = discord.Embed(title=prize, description=f"time: <t:{convert_to_unix_time(date, days, hours, minutes, seconds)}:R>")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.