A user is specifying an hour and minute in my interface. I’d like to attach the current date to a datetime object in Python.
First I tried this:
a = datetime.strptime("8:30pm", "%I:%M%p")
print a
But this yields:
1900-01-01 20:30:00
Then I tried:
b = date.today()
a = datetime.strptime(str(b.year) + "-" + str(b.month) + "-"
+ str(b.day) + " 8:30pm", "%Y-%m-%d %I:%M%p")
This works, but it’s ugly; surely there is a better way?
Yes, use datetime.datetime.combine: