Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Python by (47.6k points)

Is there a built-in method for converting a date to a datetime in Python, for example getting the datetime for the midnight of the given date? The opposite conversion is easy: datetime has a .date() method.

Do I really have to manually call datetime(d.year, d.month, d.day)?

1 Answer

0 votes
by (106k points)

To convert date to datetime in Python you can use datetime.combine(date, time) following is the code that explains how to do it:-

from datetime import date

from datetime import datetime

dt = datetime.combine(date.today(), datetime.min.time())

print(dt)

image

Related questions

+2 votes
2 answers
0 votes
1 answer
0 votes
1 answer

Browse Categories

...