0

I am using Python's datetime lib to parse a date and create a datetime object from it.

datetime.datetime.strptime(request.GET['date'], '%d-%m-%Y').date()

This works as long as a valid date is passed. I could use a try and except case to handle invalid dates, but would like to return the next valid date, as this is a Django view where a redirect would make sense.

How could I solve this?

Hi Hi
  • 366
  • 4
  • 20
  • 5
    What does "next valid date" mean? How would you determine the next valid date for a completely bogus input? – solarissmoke Jun 22 '16 at 15:51
  • Hm... you are right. I just couldn't come up with an idea yesterday, but I think I could just do it by checking for some cases. Years below 2010 would have no relevance to me and I could just change it to 2010 or current year. A year > current year+1 to current year+1. Months that are "0" to January, Months > 12 to December. This would already solve my problem. for months and years. And I think I could also handle days with something like this: http://stackoverflow.com/a/4945362/4758860 – Hi Hi Jun 23 '16 at 07:15

0 Answers0