The following code works fine in converting two search criteria to datetimes:
for _key in request.GET: if _key == "txtCriteriaDateFrom": _dateFromStr = request.GET[_key] _dateFrom = datetime.strptime(_dateFromStr, '%Y-%m-%d') if _key == "txtCriteriaDateTo": _dateToStr = request.GET[_key] _dateTo = datetime.strptime(_dateToStr, '%Y-%m-%d')
The data is coming into the GET object from a <input type=‘date’ control and that wants the Y-m-d format.
I’m just a little concerned that the format is very specific and that makes the above code very brittle and liable to break in the future.
Is there a more generic way of converting a string with a valid datetime format, but which could be any format (the US standard m-d-Y being a good example)?
Or is there a better way of doing this completely?