How to make an unaware datetime timezone aware in python

前端 未结 12 2209
一向
一向 2020-11-22 08:39

What I need to do

I have a timezone-unaware datetime object, to which I need to add a time zone in order to be able to compare it with other timezon

12条回答
  •  自闭症患者
    2020-11-22 09:03

    quite new to Python and I encountered the same issue. I find this solution quite simple and for me it works fine (Python 3.6):

    unaware=parser.parse("2020-05-01 0:00:00")
    aware=unaware.replace(tzinfo=tz.tzlocal()).astimezone(tz.tzlocal())
    

提交回复
热议问题