burning
burning

Reputation: 2586

Inserting python datetime into the mysql table

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 .

Upvotes: 0

Views: 222

Answers (1)

ChrisGuest
ChrisGuest

Reputation: 3608

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)

Upvotes: 1

Related Questions