Search code examples
pythondiscorddiscord.pypycord

How would i get the time to be first and the date last but also include the timezone?


Hi im trying to get my bot made with discord.py to have a good morning command and say the date and time but the date is first and the time is last but also its in military time and the year is first for the date how would i fix this? Response from bot

@client.command(aliases=["gm"])
async def goodmorning(ctx):
    embed = discord.Embed(description= f"Good Morning, it is currently {ctx.message.created_at}", colour = 0x8bc1a9)
    
    discord.Embed.timestamp = ctx.message.created_at

    await ctx.send(embed = embed)

Solution

  • Message.created_at is a datetime.datetime object, so you can use datetime.datetime.strftime, which will format the datetime as a string in a format you pass. You can find the formatting codes here.

    So, for example, if I wanted the datetime in the MM/DD/YYYY HH:MM AM/PM format, I would use:

    formatted_datetime = ctx.message.created_at.strftime("%m/%d/%Y %I:%M %p")
    embed = discord.Embed(description=f"Good Morning, it is currently {formatted_datetime}", colour=0x8bc1a9)
    embed.timestamp = ctx.message.created_at
    await ctx.send(embed=embed)
    

    As a side note, discord.Embed is a reference to the Embed class, not your Embed object. So, to set the timestamp for your object, you'll want to use embed.timestamp as I did in the above example (as embed is a reference to your object).