need to convert UTC (aws ec2) to PST in python

前端 未结 5 1018
星月不相逢
星月不相逢 2020-12-08 15:58

I need to convert UTC time, (on ec2 instance) to PST. I am trying to do this.

from datetime import datetime
from pytz import timezone
import pytz

date_form         


        
5条回答
  •  佛祖请我去吃肉
    2020-12-08 16:27

    I think you want datetime.utcnow() if you are attempting to simulate UTC time in your example.

    The other issue is that by default the object has no timezone. This does not mean that it is UTC, and I think pytz is just defaulting to localtime for that object. You need to create a new datetime object with the timezone set as UTC before you try to convert it to PST.

    You can do this via

    date = datetime.utcnow()
    date.replace(tzinfo=pytz.utc)
    

提交回复
热议问题