How to get python to display current time (eastern)

后端 未结 4 1181
挽巷
挽巷 2021-02-07 07:17

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.

4条回答
  •  不知归路
    2021-02-07 07:42

    Pytz library should be useful. Using Pytz (supports > 2.3) below code can get you the time according to eastern timezone.

    from datetime import datetime, timedelta
    from pytz import timezone
    
    eastern = timezone('US/Eastern')
    fmt = '%Y-%m-%d %H:%M:%S %Z%z'
    loc_dt = eastern.localize(datetime(2012, 10, 29, 6, 0, 0))
    print loc_dt.strftime(fmt)
    

提交回复
热议问题