How to convert datetime.date to datetime.datetime in Python

According to this

PyMongo doesn’t support saving datetime.date instances, since there is no BSON type for dates without times. Rather than having the driver enforce a convention for converting datetime.date instances to datetime.datetime instances for you, any conversion should be performed in your client code.

So you can use datetime.combine to combine your datetime.date and datetime.time(0, 0)

from datetime import date
from datetime import datetime
d = date.today()
datetime.combine(d, datetime.min.time())
comments powered by Disqus