Datstore data for a Google application, date to date in Python?

I always hated the headache of managing dates, times, dates, and the various formats and conversions that are needed with them. I’m taking an online course on using the Google engine for Google, and it says that it uses the datetime property, which returns a date in the format:

2012-06-25 01:17:40.273000

I tried

datetime.strptime('2012-06-25 01:17:40.273000','%y-%m-%d %H:%M:%S')

but it didn’t work.

I just want to extract part 2012-06-25 without using a hacker regular expression or a line cutting solution.

How to parse and convert it to the appropriate format?

+4
source share
2 answers

If you use the datetime property, the returned object is an instance of datetime, not a string.

On console

 >>> from datetime import datetime >>> x = datetime.now() >>> print x 2012-06-25 12:03:15.835467 >>> x.date() datetime.date(2012, 6, 25) >>> >>> print x.date() 2012-06-25 >>> 

See print instructions. It does an implicit conversion to string. If you specify the value of the datetime property in the template, this is likely to happen.

Thus, in your code, you should simply use the .date () method for the datetime object.

+5
source

Finally found it (shortly after the question, but I tried in the last hour)

 datetime.strptime('2012-06-25 01:17:40.273000','%Y-%m-%d %H:%M:%S.%f') 

What I wanted:

 datetime.strptime('2012-06-25 01:17:40.273000','%Y-%m-%d %H:%M:%S.%f').strftime('%m-%d-%Y') 
+5
source

All Articles