Ask coding questions

← Back to all posts
timezone in datetime
OfficerGhost (0)

im looking to change the timezone to EST instead of london time

Answered by IMayBeMe (40) [earned 5 cycles]
View Answer
Comments
hotnewtop
IMayBeMe (40)

Try to use pytz.timezone("your_timezone")
This should change it to the desired timezone.

OfficerGhost (0)

@IMayBeMe do i need to import anything?

IMayBeMe (40)

@OfficerGhost I think you need to import pytz

OfficerGhost (0)

@IMayBeMe my output to discord is still london time

IMayBeMe (40)

@OfficerGhost Did you set it in a variable and use that variable?

IMayBeMe (40)

@OfficerGhost
For example

x = pytz.timezone("EST")
x = datetime.datetime.now()
OfficerGhost (0)

@IMayBeMe where would i put x in my output

IMayBeMe (40)

@OfficerGhost I'm not sure what you mean just try to put that line above where you have x = datetime.datetime.now()

OfficerGhost (0)

@IMayBeMe if you look i have a message.content.startswith ('datetime') await message.channel.send ('output')
how do i add my timezone to my output

IMayBeMe (40)

@OfficerGhost or maybe put this bellow x = datetime.datetime.now()

timezone = pytz.timezone("EST")
x_aware = timezone.localize(x)
IMayBeMe (40)

@OfficerGhost You can't, you have to do it through code and it should output the correct timezone.

OfficerGhost (0)

@IMayBeMe nvm this thing has pissed me off lol

RYANTADIPARTHI (5710)

maybe try this code to change it.

from datetime import datetime
from pytz import timezone

fmt = "%Y-%m-%d %H:%M:%S %Z%z"
timezonelist = ['UTC','US/Pacific','Europe/Berlin']
for zone in timezonelist:

    now_time = datetime.now(timezone(zone))
    print now_time.strftime(fmt)

here's a link for more info.

https://stackoverflow.com/questions/10997577/python-timezone-conversion