Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to convert datetime to date in Python

  • Last Updated : 23 Aug, 2021

In this article, we are going to see how to convert DateTime to date in Python. For this, we will use the strptime() method. This method is used to create a DateTime object from a string. Then we will extract the date from the DateTime object using the date() function.

Syntax: datetime.strptime()

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Parameters : 



  • arg: It can be integer, float, tuple, Series, Dataframe to convert into datetime as its datatype
  • format: This will be str, but the default is None. The strftime to parse time, eg “%d/%m/%Y”, note that “%f” will parse all the way up to nanoseconds.

Example 1: Convert datetime to date.

In this example, We have created a datetime_str which is “24AUG2001101010”, and its format is “%d%b%Y%H%M%S”.

Python3




# import important module
import datetime
from datetime import datetime
  
# Create datetime string
datetime_str = "24AUG2001101010"
print("datetime string : {}".format(datetime_str))
  
# call datetime.strptime to convert
# it into datetime datatype
datetime_obj = datetime.strptime(datetime_str, 
                                 "%d%b%Y%H%M%S")
  
# It will print the datetime object
print(datetime_obj)
  
# extract the time from datetime_obj
date = datetime_obj.date()
print(date)

Output : 

datetime string : 24AUG2001101010
2001-08-24 10:10:10
2001-08-24

Example 2: Convert datetime with numeric date.

In this example, We have created a datetime_str which is “100201095407”, and its format is “%d%m%y%H%M%S”.

Python3




# import important module
import datetime
from datetime import datetime
  
# Create datetime string
datetime_str = "100201095407"
print("datetime string : {}".format(datetime_str))
  
# call datetime.strptime to convert
# it into datetime datatype
datetime_obj = datetime.strptime(datetime_str,
                                 "%d%m%y%H%M%S")
  
# It will print the datetime object
print(datetime_obj)
  
# extract the time from datetime_obj
date = datetime_obj.date()
  
# it will print date that we have
# extracted from datetime obj
print(date)

Output

datetime string : 100201095407
2001-02-10 09:54:07
2001-02-10

Example 3: Convert datetime with current date.

In this example, we take present date and time, and extracted its date from the object.

Python3




# import important module
from datetime import datetime
  
# call datetime.strptime to
# convert it into datetime datatype
datetime_obj = datetime.now()
  
# It will print the datetime object
print(datetime_obj)
  
# extract the time from datetime_obj
date = datetime_obj.date()
print(date) 

Output: 

2021-08-07 06:30:20.227879
2021-08-07



My Personal Notes arrow_drop_up
Recommended Articles
Page :