0

I have a Python date like

2013-04-04T18:56:21Z

I want to store this into my mysql database .

I tried like

self.start_at = datetime.strptime(self.start_at.split(".")[0], "%Y-%m-%dT%H:%M:%S")

But i am getting an error

ValueError: unconverted data remains: Z

PLease tell me how to convert the above date in mysql acceptable format .

1 Answer 1

1

In this instance this will work.

>>> from datetime import datetime
>>> start_at = '2013-04-04T18:56:21Z'
>>> datetime.strptime(start_at , "%Y-%m-%dT%H:%M:%SZ")
datetime.datetime(2013, 4, 4, 18, 56, 21)

Are the T & Z characters always in your date format or do they every change? If the is some variation in these seperator characters, you should do something like this:

>>> from datetime import datetime
>>> start_at = '2013-04-04T18:56:21Z'
>>> datetime.strptime(start_at[0:10] + ' ' + start_at[11:19], "%Y-%m-%d %H:%M:%S")
datetime.datetime(2013, 4, 4, 18, 56, 21)
Sign up to request clarification or add additional context in comments.

2 Comments

I am getting this TypeError: 'datetime.datetime' object has no attribute 'getitem'
I've amended the code, to clarify the assumptions involved. This should now run as a standalone python 2.* example.

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.