Python: timezone.localize() not working

前端 未结 2 1848
孤街浪徒
孤街浪徒 2021-01-07 22:13

I am having some issues getting timezone.localize() to work correctly. My goal is to grab today\'s date and convert it from CST to EST. Then finally format the

2条回答
  •  再見小時候
    2021-01-07 22:24

    Use cst.localize to make a naive datetime into a timezone-aware datetime.

    Then use astimezone to convert a timezone-aware datetime to another timezone.

    import pytz
    import datetime
    
    est = pytz.timezone('US/Eastern')
    cst = pytz.timezone('US/Central')
    curtime = cst.localize(datetime.datetime.now())
    curtime = curtime.astimezone(est)
    

提交回复
热议问题