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 then we will convert it to date.
Environment and requirements-
Program for date to datetime conversion
from datetime import date from datetime import datetime mdate=date.today() print("Today's Date is : ",mdate) mtime=datetime.now().time() print("Current Time is : ",mtime) mdatetime=datetime.combine(mdate,mtime) print("Current Datetime is : ", mdatetime)
Today’s Date is : 2023-01-05
Current Time is : 18:01:13.274525
Current Datetime is : 2023-01-05 18:01:13.274525
Program 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.