It should be easy, but as always, Python's wildly overly complex datetime mess makes simple things complicated ...
So, I have a temporary line in the format HH: MM (for example, '09: 30 '), which I would like to include in the date and date with today's date. Unfortunately, the default date is January 1, 1900:
>>> datetime.datetime.strptime(time_str, "%H:%M") datetime.datetime(1900, 1, 1, 9, 50)
datetime.combine looks like it meant exactly that, but I'll be damned if I can figure out how to get the time analyzed so it will take it:
now = datetime.datetime.now() >>> datetime.datetime.combine(now, time.strptime('09:30', '%H:%M')) TypeError: combine() argument 2 must be datetime.time, not time.struct_time >>> datetime.datetime.combine(now, datetime.datetime.strptime('09:30', '%H:%M')) TypeError: combine() argument 2 must be datetime.time, not datetime.datetime >>> datetime.datetime.combine(now, datetime.time.strptime('09:30', '%H:%M')) AttributeError: type object 'datetime.time' has no attribute 'strptime'
This monster is working ...
>>> datetime.datetime.combine(now, datetime.time(*(time.strptime('09:30', '%H:%M')[3:6]))) datetime.datetime(2014, 9, 23, 9, 30)
... but there should be a better way to do this ...!?