In this article, we are going to learn how we can convert date object to datetime. We will make use of the datetime module of Python to get the current date and time. Once we have current datetime it then we will convert it to date.
Python program to convert date to datetime
from datetime import date
from datetime import datetime
mdate=date.today()
print(“Today’s Date: “,mdate)
mtime=datetime.now().time()
print(“Current Time: “,mtime)
mdatetime=datetime.combine(mdate,mtime)
print(“Datetime: “, mdatetime)
Output:
Today’s Date: 2023-02-18
Current Time: 19:37:55.779312
Datetime: 2023-02-18 19:37:55.779312
Source Code Explanation
- First we import datetime class from datetime module.
- Get the current date and print the date.
- Get the current time and print the current time.
- Combine the date and time and print combined date and time.