parsing date string in python (convert string to date)

前端 未结 1 1087
误落风尘
误落风尘 2021-01-03 01:34

I have a datetime string in the form of a string as:

2011-10-23T08:00:00-07:00

How do i parse this string as the datetime object.

I

相关标签:
1条回答
  • 2021-01-03 01:49

    Standard datetime.datetime.strptime has problems with timezone definitions. Use dateutil.parser

    >>> from dateutil import parser
    >>> parser.parse("2011-10-23T08:00:00-07:00")
    datetime.datetime(2011, 10, 23, 8, 0, tzinfo=tzoffset(None, -25200))
    

    If you care about the date part only, you can try it without dateutil.parser:

    >>> from datetime import datetime
    >>> datetime.strptime(data[4].partition('T')[0], '%Y-%m-%d').date()
    datetime.date(2011, 10, 23)
    
    0 讨论(0)
提交回复
热议问题