How to parse dates with -0400 timezone string in Python?

后端 未结 6 1377

I have a date string of the form \'2009/05/13 19:19:30 -0400\'. It seems that previous versions of Python may have supported a %z format tag in strptime for the trailing tim

6条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 09:47

    You can use the parse function from dateutil:

    >>> from dateutil.parser import parse
    >>> d = parse('2009/05/13 19:19:30 -0400')
    >>> d
    datetime.datetime(2009, 5, 13, 19, 19, 30, tzinfo=tzoffset(None, -14400))
    

    This way you obtain a datetime object you can then use.

    As answered, dateutil2.0 is written for Python 3.0 and does not work with Python 2.x. For Python 2.x dateutil1.5 needs to be used.

提交回复
热议问题