Write a Python program to convert the date to datetime (midnight of the date) in Pytho


This program combines the current date with the minimum time of the datetime object and prints the resulting datetime object.

  • from datetime import date and from datetime import datetime import the required modules from the datetime library.
  • dt = date.today() initializes dt with the current date using the today() method of the date class.
  • datetime.combine(dt, datetime.min.time()) combines the dt date object with the minimum time of a datetime object, i.e., midnight, using the combine() method of the datetime class. The resulting object is a datetime object with the same date as dt and the time set to midnight.
  • print() statement prints the resulting datetime object.

Source Code

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

Output

2022-09-24 00:00:00

Example Programs