In the following code, I am trying to convert 2 types of date formats into a common date format so that I can store it in my database.
import datetime
Date1 = '2012-04-24T12:58:52Z'
Date2 = 'Tue, 24 Apr 2012 12:56:21 -0700'
date1 = Date1.replace("T"," ").replace("Z","")
y = datetime.datetime.strptime(date1,"%Y-%m-%d %H:%M:%S")
date1 = datetime.datetime.strftime(y,"%A, %b %d %Y %H:%M:%S %p")
k = Date2.split(" ")
y = " ".join(k[1:len(k)-1])
date2 = datetime.datetime.strptime(y,"%d %b %Y %H:%M:%S")
date2 = datetime.datetime.strftime(date2,"%A, %b %d %Y %H:%M:%S %p")
print date1
print date2
It gives me the following output.
Tuesday, Apr 24 2012 12:58:52 PM
Tuesday, Apr 24 2012 12:56:21 PM
But when I try to save it in the Database, it is throwing this error.
Enter a valid date/time in YYYY-MM-DD HH:MM[:ss[.uuuuuu]] format
Can any1 please help me solving this issue. Thanks in advance.