On 11/05/2012 12:13, Nikhil Verma wrote:
Hi All

I was going through this link
http://docs.python.org/library/datetime.html#strftime-strptime-behavior.
I practised strftime() and strptime() functions.

Finally i stuck into a situation where  i want to get the datetime
object so that i can save it in my db.
What i want is :-

I have a string let say
date_created = '11 May Friday PM '

and i want to convert it into datetime object like this
datetime.datetime(2012, 5, 11, 4, 12, 44, 24734)

Thanks in advance. Any help will be appreciated

As it says in the documentation:

    %d matches the day of the month
    %B matches the name of the month
    %A matches the day of the week
    %p matches AM/AM

so:

datetime.datetime.strptime('11 May Friday PM', '%d %B %A %p')
datetime.datetime(1900, 5, 11, 0, 0)

(I've stripped off the trailing space.)

Note that as you haven't supplied a year, the year defaults to 1900,
and as you haven't supplied an hour but only "PM", the hour defaults to
0 (and there's no way to tell whether it was AM or PM). (The minutes
and seconds also default to 0.)

You can't convert something as vague as '11 May Friday PM' to a
datetime object.

If all of the times are just AM/PM you could try appending an hour (eg
'12') before parsing and ignore it when you convert it back to a string
for display.
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to