How can I convert a timestamp string with timezone offset to local time?

后端 未结 3 2182
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-14 18:00

I am trying to convert a string timestamp into a proper datetime object. The problem I am having is that there is a timezone offset and everything I am doing doesn\'t seem to wo

3条回答
  •  攒了一身酷
    2021-02-14 18:30

    The dateutil package is handy for parsing date/times:

    In [10]: date = u"Fri, 16 Jul 2010 07:08:23 -0700"
    
    In [11]: from dateutil.parser import parse
    
    In [12]: parse(date)
    Out[12]: datetime.datetime(2010, 7, 16, 7, 8, 23, tzinfo=tzoffset(None, -25200))
    

    Finally, to convert into your local timezone,

    In [13]: parse(date).astimezone(YOUR_LOCAL_TIMEZONE)
    

提交回复
热议问题