Hello I am using Pythonanywhere and when I call
from datetime import *
print date.today().day
It is printing a different day than the day it is where I live (Austin, Texas). I figured it is because there is a timezone difference. How would I tell the date object where I live so it gets the right timezone. Thanks in advance
The most robust way to do this is to use pytz
. You can install it simply with pip install pytz
.
To get the local date with pytz
, you can simply do this (note that the date.today
method will not take a timezone):
>>> from datetime import datetime
>>> import pytz
>>> local_date = datetime.now(pytz.timezone('US/Central')) # use datetime here
>>> local_date.date() # now call date method
datetime.date(2014, 11, 30)
Compare that to the present Greenwich date:
>>> greenwich_date = datetime.now(pytz.timezone('Etc/Greenwich'))
>>> greenwich_date.date()
datetime.date(2014, 12, 1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With