Convert datetime to date in Python

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

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