Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get python to display current time (eastern)

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.

like image 930
Hyzenthlay Avatar asked Jul 29 '12 15:07

Hyzenthlay


People also ask

How do I get the current time in Python est?

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.

How do I get different time zones in Python?

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.

How do I get Indian Standard time in Python?

datetime. now() will return the correct time if your computer's date/time is set correctly.


1 Answers

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)
like image 192
bmbigbang Avatar answered Oct 19 '22 15:10

bmbigbang