I'm cocking this up and it should be really simple but the value of sortdate is none (note im only doing this because converting a string to a date in Python is a bugger).
DateToPass = str(self.request.get('startdate'))
开发者_JAVA百科mybreak.startdate = DateToPass
faf = DateToPass.split('-')
sortdate = str(faf[2] + faf[1] + faf[0])
That should work? but its just being stored as null though the datetopass is being stored fine.
It would be helpful to see what self.request.get('startdate')
looked like. Is it ISO (YYYY-MM-DD
)? If so I'll show an example using datetime
. There's no need for splits because of datetime.datetime.strptime
:
>>> import datetime
>>> date_to_pass = '2010-05-07'
>>> sortdate = datetime.datetime.strptime(date_to_pass, '%Y-%m-%d')
>>> sortdate
datetime.datetime(2010, 5, 7, 0, 0)
Datetime objects are sortable, so there's no need to convert to a string. Unless I'm missing the point of your question.
If your input date is in the format 'YYYY-MM-DD' then the code you have shold work fine. There are a few extraneous str() calls, and yeah, it'd be more proper to use strptime, but nothing that should break.
For example, this works:
Python 2.5.2 (r252:60911, Apr 15 2008, 11:28:25)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> startdate = '2002-04-20'
>>> splitdate = startdate.split('-')
>>> type(splitdate[0])
<type 'str'>
>>> splitdate[2]+splitdate[1]+splitdate[0]
'20042002'
So the two places I'd look are:
- What is the format you get from self.request.get('startdate') and store in DateToPass?
- You haven't shown us the code where you store sortdate. Is it broken?
If the real issue is converting a string to a time, as you have indicated, then have you looked into time.strptime
?
精彩评论