Reputation: 6554
I have this string:
2014-03-13T15:10:07Z
And I need convert it to 'value', that I can save in Django DateTimeField. I know, how to do this without T
and Z
:
from datetime import datetime
date_object = datetime.strptime('2014-03-13 15:10:07', '%Y-%m-%d %H:%M:%S')
Does I need to delete T
and Z
from string? Will it saves in DateTimeField?
Thanks.
Upvotes: 2
Views: 318
Reputation: 19780
The easiest way to parse a datetime string is with dateutil.parser.parse()
from the third-party dateutil module:
>>> import dateutil.parser
>>> dateutil.parser.parse('2014-03-13T15:10:07Z')
datetime.datetime(2014, 3, 13, 15, 10, 7, tzinfo=tzutc())
This returns a datetime
instance which will be aware of the timezone if it is present in the string (i.e., ending with Z
, -04:00
, etc.).
Upvotes: 4