I have a web form which has 2 input fields, "StartDate" and "StartTime". I convert the StartDate field's string into a Python datetime object, no problem. The StartTime field is passed in as a string in the form "0130" for 1:30am. What is the best way to convert the StartTime string and combine it with the StartDate datetime object so that both are stored as a single datetime?
4 Answers
Use datetime.combine:
import datetime as dt
mytime = dt.datetime.strptime('0130','%H%M').time()
mydatetime = dt.datetime.combine(dt.date.today(), mytime)
1 Comment
mechanical_meat
A note on readability: When the module name and class name are the same (in this case
datetime) I prefer to use an alias to clarify.
datetime.combine