2

time = "2019-02-20T04:35:48.679000+00:00"

>>> import datetime
>>> 
>>> date_time_str = "2019-02-20T04:35:48.679000+00:00"
>>> date_time_obj = datetime.datetime.strptime(date_time_str, '%Y-%m-%d %H:%M:%S.%f')

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.5/_strptime.py", line 510, in _strptime_datetime
    tt, fraction = _strptime(data_string, format)
  File "/usr/lib/python3.5/_strptime.py", line 343, in _strptime
    (data_string, format))
ValueError: time data '2019-02-20T04:35:48.679000+00:00' does not match format '%Y-%m-%d %H:%M:%S.%f'

The time is Django format time. Unfortunately i am storing the same in an CharField() so it is giving time as a string while fetching data from database.

How can i convert it into actual time.

2 Answers 2

3

You need to account for the 'T', and I don't know how to match the "+00:00" part unless it's simply a literal that's always that value. So this works:

import datetime
date_time_str = "2019-02-20T04:35:48.679000+00:00"
date_time_obj = datetime.datetime.strptime(date_time_str, '%Y-%m-%dT%H:%M:%S.%f+00:00')
Sign up to request clarification or add additional context in comments.

1 Comment

Ok, I admit it...@Mike's answer is what you want :) . ... unless you don't want to require python-dateutil for some reason.
2

try to use python-dateutil

install it pip install python-dateutil

and then

from dateutil import parser
dt = parser.parse("2019-02-20T04:35:48.679000+00:00")

1 Comment

hi @kiran could you choose one answer you prefer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.