attribute 'tzinfo' of 'datetime.datetime' objects is not writable

后端 未结 3 594
一生所求
一生所求 2021-02-05 02:01

How do I set the timezone of a datetime instance that just came out of the datastore?

When it first comes out it is in UTC. I want to change it to EST.

I\'m try

3条回答
  •  南方客
    南方客 (楼主)
    2021-02-05 02:46

    datetime's objects are immutable, so you never change any of their attributes -- you make a new object with some attributes the same, and some different, and assign it to whatever you need to assign it to.

    I.e., in your case, instead of

    book.creationTime.tzinfo = EST
    

    you have to code

    book.creationTime = book.creationTime.replace(tzinfo=EST)
    

提交回复
热议问题