Nicholas Chen
Nicholas Chen

Reputation: 144

discord.py current time command

I have this time command, and I want it so that when I say p!time, it would say PST time:, EST time:, etc

if message.content.startswith('p!time'):
        timestamp = datetime.now()
        await message.channel.send(timestamp)
        await message.channel.send(timestamp.strftime(r"%I:%M %p"))
        #now idk what to do

Upvotes: 0

Views: 1205

Answers (1)

Just for fun
Just for fun

Reputation: 4225

You need to change timezone, it can be done using a library called pytz

$ pip install pytz

Code:

import pytz
from datetime import datetime

if message.content.startswith('p!time'):
    timestamp = datetime.now()
    pst = pytz.timezone('US/Pacific')
    est = pytz.timezone('US/Eastern')
    msg = f"PST Time: {timestamp.astimezone(pst).strftime("%I:%M %p")}, EST Time: {timestamp.astimezone(est).strftime("%I:%M %p")}"
    await message.channel.send(msg)

Upvotes: 2

Related Questions