Python: most efficient way to convert date to datetime

Python: most efficient way to convert date to datetime

Use datetime.datetime.combine() with a time object, datetime.time.min represents 00:00 and would match the output of your date-string-datetime path:

datetime.datetime.combine(my_date, datetime.time.min)

Demo:

>>> import datetime
>>> my_date = datetime.date.today()
>>> datetime.datetime.combine(my_date, datetime.time.min)
datetime.datetime(2013, 3, 27, 0, 0)

Alternatively, as suggested here, this might be more readable:

datetime(date.year, date.month, date.day)

Python: most efficient way to convert date to datetime

Leave a Reply

Your email address will not be published.