ValueError: unconverted data remains: 02:05
The value of st
at st = datetime.strptime(st, '%A %d %B')
line something like 01 01 2013 02:05
and the strptime
can't parse this. Indeed, you get an hour in addition of the date... You need to add %H:%M
at your strptime.
Best answer is to use the from dateutil import parser
.
usage:
from dateutil import parser
datetime_obj = parser.parse('2018-02-06T13:12:18.1278015Z')
print datetime_obj
# output: datetime.datetime(2018, 2, 6, 13, 12, 18, 127801, tzinfo=tzutc())
You have to parse all of the input string, you cannot just ignore parts.
from datetime import date, datetime
for item in j:
st = datetime.strptime(item['start'], '%A %d %B %H:%M')
if st.date() == date.today():
item['start'] = st.time()
Here, we compare the date to today's date by using more datetime
objects instead of trying to use strings.
The alternative is to only pass in part of the item['start']
string (splitting out just the time), but there really is no point here, not when you could just parse everything in one step first.