textdiscordnextcord

How do you use Unix timestamps inside discord messages


Formatted Dates in a Discord Message

I'm trying to use Unix Timestamps within a Discord Message, can't find any information about it.

Thank you!


Solution

  • See format_dt in the docs.

    Example:

    from nextcord.utils import format_dt
    from datetime import datetime
    
    dt = datetime.now()
    
    # Short Time (9:18 AM)
    short_time = format_dt(dt, "t")
    
    # Long Time (9:18:58 AM)
    long_time = format_dt(dt, "T")
    
    # Short Date (04/03/2022)
    short_date = format_dt(dt, "d")
    
    # Long Date (April 3, 2022)
    long_date = format_dt(dt, "D")
    
    # Default (April 3, 2022 9:18 AM)
    default_date_time = format_dt(dt, "f")
    
    # Long Date Time (Sunday, April 3, 2022 9:18 AM)
    long_date_time = format_dt(dt, "F")
    
    # Relative Time (in 5 minutes / 1 day ago)
    relative = format_dt(dt, "R")
    
    ...
    
    # To send this in a command:
    await ctx.send(f"{default_date_time} ({relative})")