How can I get Python to display the time in eastern?
I've looked over the python documentation but it's pretty confusing. I'm using Python 3.
Thanks.
To get the current time in particular, you can use the strftime() method and pass into it the string ”%H:%M:%S” representing hours, minutes, and seconds.
Use the datetime. astimezone() method to convert the datetime from one timezone to another. This method uses an instance of the datetime object and returns a new datetime of a given timezone.
datetime. now() will return the correct time if your computer's date/time is set correctly.
There is a much more intuitive way, of course:
from datetime import datetime
from pytz import timezone
tz = timezone('EST')
datetime.now(tz)
## this returns a datetime object pointing to right now
## according to the timezone info object handed in as the tz variable.
Alternatively you can define your own datetime
object and pass in tz
as tzinfo
, as you can see below:
datetime(2016, 3, 30, 11, 13, 24, tzinfo=tz)
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