In this article, we are going to learn how to convert Datetime to date in Python. We will make use of the datetime module of Python to get the current date and time. Once we have the current datetime it then we will convert it to date.
Environment and requirements-
- Python interpreter
- datetime library
Python Program to Convert datetime to date
- First, we import the datetime class from the datetime module.
- Asked the User to the datetime object in the specified format.
- Convert string into datetime using strptime method.
- Print datetime.
- Finally, Extract date from the datetime.
Related Post
- 6 ways to get current Timestamp in Python
- How to convert DateTime to string in Python
- How to convert string to datetime in Python
- Unix Timestamp to datetime in Python
- Get the Current Year, month Python
- Get the Current Time in Python
- Convert Date Time to String with Milliseconds
- Convert Epoch to Datetime in Python
- Convert Datetime to Epoch Python
- Get the Current Date in Python
- Get the Day of the Week in Python
- Convert datetime to string in Python
- Compare Two Dates in Python
- Calculate Time Difference Between Time Strings
- Convert date to datetime in Python
- Get the Time zones List Using Python
import datetime
from datetime import datetime
print("Please enter the date and time:")
datetime_str=input()
format="%m/%d/%y %H:%M:%S"
datetime_obj = datetime.strptime(datetime_str,format)
date = datetime_obj.date()
print("date",date)
Output-
Please enter the date and time:
1/4/23 2:5:9
date 2023-01-04