1

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.

3
  • have you tried to adapt strftime to match the required format? Commented May 3, 2012 at 11:11
  • Hi @moooeeeep, I have tried doing that. But this time, a different error showed up. "time data '2012-05-02 19:44:34' does not match format '%A, %b %d %Y %H:%M:%S %p'" Commented May 3, 2012 at 11:14
  • you missed the 'T', see below answers for correct format Commented May 3, 2012 at 11:16

4 Answers 4

2

Many DBs accept datetime.datetime objects.

import datetime

Date1 = '2012-04-24T12:58:52Z'
dt1 = datetime.datetime.strptime(Date1, '%Y-%m-%dT%H:%M:%SZ')
# dt1: datetime.datetime(2012, 4, 24, 12, 58, 52)

and now try to insert dt1 into DB as an object.

Sign up to request clarification or add additional context in comments.

Comments

0

I would store the times as plain integers, in seconds since epoch (UNIX timestamp). This way, you can store it into any database without hassle. The %s format specifier for strftime returns the UNIX timestamp as string—so something along the lines of

int(datetime.datetime.now().strftime('%s'))

should work. You can then convert the timestamp back to a datetime object using datetime.datetime.fromtimestamp(…).

2 Comments

Probably he has a specific database, though he didn't stated. You lose features like postgreSQL current_date and likewise converting all times to seconds since epoch.
True, the use case defines whether this approach is feasible or not.
0

Use this format for date1 and date2:

date1 = datetime.datetime.strftime(y,"%Y-%m-%dT%H:%M:%S")
date2 = datetime.datetime.strftime(date2,"%Y-%m-%dT%H:%M:%S")

this will result in:

2012-04-24T12:58:52
2012-04-24T12:56:21

the error message you report clearly states that you need this format.

2 Comments

Hi @snies, the have this solution earlier but the following error popped up while inserting into db "time data '2012-04-24 12:58:52' does not match format '%A, %b %d %Y %H:%M:%S %p'"
i am a bit confused, maybe you edited your question, but i think you had a 'T' delimiting the date and the time in your original error description. Please make sure what your error says and what your format string looks like. Subtle differences do matter. But it's probably best if you just post the db you are using.
0

I'm not exactly sure which object you're trying to store in the DB. As eumiro suggests, many libraries let you store the datetime object directly. But assuming you need to store it as a string, but why can't you just format the datetime object as the database error suggests (YYYY-MM-DD HH:MM:ss)?

So ... like this:

 import datetime

 Date1 = '2012-04-24T12:58:52Z'
 d1 = datetime.datetime.strptime(Date1,"%Y-%m-%dT%H:%M:%SZ")
 d1.strftime("%Y-%m-%d %H:%M:%S") # Store this!

 Date2 = 'Tue, 24 Apr 2012 12:56:21 -0700'
 k = Date2.split(" ")
 y = " ".join(k[1:len(k)-1])
 d2 = datetime.datetime.strptime(y,"%d %b %Y %H:%M:%S")
 d2.strftime("%Y-%m-%d %H:%M:%S") # Store this!

This may be outside the scope of your question, but note that with Date2, you're stripping out the timezone information (-0700). If that's important to you, you should parse and store that somehow. There's a %z option in strptime that parses this, but it doesn't seem to work on all platforms unfortunately.

2 Comments

I have done the same thing earlier also. It gave me this error. "time data '2012-04-24 12:58:52' does not match format '%A, %b %d %Y %H:%M:%S %p'"
Looks like you're using Django and MySQL. You should be able to just insert the datetime object directly, without converting it to a string, as eumiro suggests. Can you paste the code where you set the datetime attribute and save?

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.