Skip to content
Sign upLog in
← Back to Community

timezone in datetime

Profile icon
OfficerGhost

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

Answered by IMayBeMe [earned 5 cycles]
View Answer
Voters
Profile icon
OfficerGhost
Comments
hotnewtop
Profile icon
IMayBeMe

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

Profile icon
OfficerGhost

@IMayBeMe
do i need to import anything?

Profile icon
IMayBeMe

@OfficerGhost
I think you need to import pytz

Profile icon
OfficerGhost

@IMayBeMe
it didnt work

Profile icon
OfficerGhost

@IMayBeMe
my output to discord is still london time

Profile icon
IMayBeMe

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

Profile icon
IMayBeMe

@OfficerGhost

For example

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

@IMayBeMe
where would i put x in my output

Profile icon
IMayBeMe

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

Profile icon
OfficerGhost

@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

Profile icon
IMayBeMe

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

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

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

Profile icon
OfficerGhost

@IMayBeMe
nvm this thing has pissed me off lol

Profile icon
RYANTADIPARTHI

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