How to convert datetime to date in Python
Last Updated :
22 Aug, 2022
In this article, we are going to see how to convert DateTime to date in Python. For this, we will use the strptime() method and Pandas module. 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 and dt.date from Pandas in Python.
Method 1: Convert DateTime to date in Python using DateTime
Classes for working with date and time are provided by the Python Datetime module. Numerous capabilities to deal with dates, times, and time intervals are provided by these classes. Python treats date and DateTime as objects, so when you work with them, you're really working with objects rather than strings or timestamps.
Syntax of strptime()
Syntax: datetime.strptime()
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 a 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 the current date.
In this example, we take the 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
Method 2: Convert DateTime to date in Python using Pandas
Pandas provide a different set of tools using which we can perform all the necessary tasks on date-time data. Let’s try to understand with the examples discussed below.
Example:
The date value and the DateTime value are both displayed in the output using the print command. The DateTime values are first added to a column of a Pandas DataFrame. The DateTime value is then converted to a date value using the dt.date() function.
Python3
import pandas as pd
df = pd.DataFrame({'time': ['2022-7-16 11:05:00',
'2025-7-18 12:00:30']})
print(df)
df['time'] = pd.to_datetime(df['time']).dt.date
print(df)
Output:
time
0 2022-7-16 11:05:00
1 2025-7-18 12:00:30
time
0 2022-07-16
1 2025-07-18
Similar Reads
Convert Date To Datetime In Python When you're programming, dealing with dates and times is important, and Python provides tools to manage them well. This article is about changing dates into date times in Python. We'll explore methods that can help you switch between these two types of data. Whether you're building a website or work
3 min read
How to Convert Datetime to Date in Pandas ? DateTime is a collection of dates and times in the format of "yyyy-mm-dd HH:MM:SS" where yyyy-mm-dd is referred to as the date and HH:MM:SS is referred to as Time. In this article, we are going to discuss converting DateTime to date in pandas. For that, we will extract the only date from DateTime us
4 min read
How to convert DateTime to integer in Python Python provides a module called DateTime to perform all the operations related to date and time. It has a rich set of functions used to perform almost all the operations that deal with time. It needs to be imported first to use the functions and it comes along with python, so no need to install it s
2 min read
How to Convert String to Date or Datetime in Polars When working with data, particularly in CSV files or databases, it's common to find dates stored as strings. If we're using Polars, a fast and efficient DataFrame library written in Rust (with Python bindings), we'll often need to convert these strings into actual date or datetime objects for easier
5 min read
How to Convert Float to Datetime in Pandas DataFrame? Pandas Dataframe provides the freedom to change the data type of column values. We can change them from Integers to Float type, Integer to Datetime, String to Integer, Float to Datetime, etc. For converting float to DateTime we use pandas.to_datetime() function and following syntax is used : Syntax:
3 min read
How to Convert DateTime to UNIX Timestamp in Python ? Generating a UNIX timestamp from a DateTime object in Python involves converting a date and time representation into the number of seconds elapsed since January 1, 1970 (known as the Unix epoch). For example, given a DateTime representing May 29, 2025, 15:30, the UNIX timestamp is the floating-point
2 min read
Python - Convert excel serial date to datetime This article will discuss the conversion of an excel serial date to DateTime in Python. The Excel "serial date" format is actually the number of days since 1900-01-00 i.e., January 1st, 1900. For example, the excel serial date number 43831 represents January 1st, 2020, and after converting 43831 to
3 min read
How to Convert Integer to Datetime in Pandas DataFrame? Let's discuss how to convert an Integer to Datetime in it. Now to convert Integers to Datetime in Pandas DataFrame. Syntax of  pd.to_datetimedf['DataFrame Column'] = pd.to_datetime(df['DataFrame Column'], format=specify your format)Create the DataFrame to Convert Integer to Datetime in Pandas Check
2 min read
Convert Python datetime to epoch Epoch time is a way to represent time as the number of seconds that have passed since January 1, 1970, 00:00:00 UTC. It is also known as Unix time or POSIX time and it serves as a universal point of reference for representing dates and times. Its used in various applications like file timestamps, da
2 min read
Convert Datetime to UTC Timestamp in Python Dealing with datetime objects and timestamps is a common task in programming, especially when working with time-sensitive data. When working with different time zones, it's often necessary to convert a datetime object to a UTC timestamp. In Python, there are multiple ways to achieve this. In this ar
3 min read