Python date string to date object

Python date string to date object

You can use strptime in the datetime package of Python:

>>> import datetime
>>> datetime.datetime.strptime(24052010, %d%m%Y).date(), 5, 24)
import datetime
datetime.datetime.strptime(24052010, %d%m%Y).date()

Python date string to date object

Directly related question:

What if you have

datetime.datetime.strptime(2015-02-24T13:00:00-08:00, %Y-%B-%dT%H:%M:%S-%H:%M).date()

and you get:

Traceback (most recent call last):
  File <stdin>, line 1, in <module>
  File /usr/local/lib/python2.7/, line 308, in _strptime
    format_regex = _TimeRE_cache.compile(format)
  File /usr/local/lib/python2.7/, line 265, in compile
    return re_compile(self.pattern(format), IGNORECASE)
  File /usr/local/lib/python2.7/, line 194, in compile
    return _compile(pattern, flags)
  File /usr/local/lib/python2.7/, line 251, in _compile
    raise error, v # invalid expression
sre_constants.error: redefinition of group name H as group 7; was group 4

and you tried:

<-24T13:00:00-08:00, %Y-%B-%dT%HH:%MM:%SS-%HH:%MM).date()

but you still get the traceback above.


>>> from dateutil.parser import parse
>>> from datetime import datetime
>>> parse(2015-02-24T13:00:00-08:00)
datetime.datetime(2015, 2, 24, 13, 0, tzinfo=tzoffset(None, -28800))

Leave a Reply

Your email address will not be published. Required fields are marked *