I need a reliable way to convert formatted date strings into datetime objects so I can save into the database. I’m importing data from an RSS feed and the date strings look like this 10/31/2012 11:59:00 PM, but every once in a while one will come through with no time data, like this: 9/24/2012. Is there a way to convert the string to a datetime object regardless if the time is present or not?
I was using Date.strptime() because Date.parse() wasn’t being reliable enough for me. Here’s what I was using, which will error out when it encounters the date string without the time.
Date.strptime(i.xpath('activedate').text, '%m/%d/%Y %I:%M:%S %p').to_datetime
What would be the best way to handle this?
I recommend chronic: