Date and time in Python are not a pre-defined data type itself. The datetime module can be imported to the Python code and it provides classes used that manipulate and alter date and time in the Python code.
This article demonstrates different methods to convert datetime to date in Python.
Table of Contents
Using the date() function to convert datetime to date in Python.
In simple terms, the datetime object returns the date and time and therefore contains the year, month, day, hour, minute, second, and microsecond values, while the date object only contains the date.
We can use the date() method which is utilized to return an object of the date type.
The following code uses the date() function to convert datetime to date in Python
|
1 2 3 4 5 6 7 8 |
import datetime datetimeObj = datetime.datetime.now() print(datetimeObj,",type:",type(datetimeObj)) # Converting datetime to date in Python dateObj = datetimeObj.date() print(dateObj,",type:",type(dateObj)) |
The above code provides the following output:
2021-06-19 ,type:
Explanation
- The
datetimemodule is imported to work with date and time data types. - The
datetime.datetime.now()is used to provide the current date and time as adatetimeobject. - The
date()method is used ondatetimeObjto convert it to date object.
When we need the current date, we can also use the datetime.date.today() function directly, but it does not work if the date is not the current date.
The following code implements the datetime.date.today()
|
1 2 3 4 5 |
import datetime print(datetime.datetime.now()) print(datetime.date.today()) |
The above code provides the following output:
2021-06-19
We can see that both these functions provide the current date as the output and can be used in this case.
We can also create a date using the datetime() constructor from the datetime module. The datetime() constructor needs three parameters namely Day, Month, and Year to generate a datetime.datetime object.
The datetime() constructor also requires parameters for the time zone and time, but this is optional and the default value 0 can be provided in case it is not specified.
|
1 2 3 4 5 6 7 8 |
import datetime datetimeObj = datetime.datetime(2021,6,19) print(datetimeObj,",type:",type(datetimeObj)) # Converting datetime to date in Python dateObj = datetimeObj.date() print(dateObj,",type:",type(dateObj)) |
The above code provides the following output:
2021-06-19 ,type:
As you can see, we did not provide time while creating datetime object, so it is showing output as 00:00:00.
Further reading:
Using the dt.date function in pandas to convert datetime to date in Python.
Pandas is a library set up on top of the Python programming language and is mostly used for the purpose of Data Analysis and Machine learning. We use pandas DataFrame to create arrays that contain date and time values.
The pandas module needs to be imported to use DataFrame and create an array that contains the time column.
The following code uses the dt.date function on a pandas DataFrame to convert datetime to date in Python.
|
1 2 3 4 5 6 7 |
import pandas as pd df = pd.DataFrame({'employee': [3, 6],'time': ['2021-05-21 15:05:00','2021-05-22 18:30:27']}) print(df) df['time'] = pd.to_datetime(df['time']).dt.date print(df) |
The above code provides the following output:
0 3 2021-05-21 15:05:00
1 6 2021-05-22 18:30:27
employee time
0 3 2021-05-21
1 6 2021-05-22
As we can see, the time column after the execution of the dt.date function only displays the date and not the time.
Explanation
- A pandas
DataFrameis created and the datetime values are added to a column. - Now, the
dt.date()function is used on thetimecolumn in the recently createdDataFrameto convert datetime value to a date value. - Both the datetime value and the date value is returned in the output using the
printcommand.
That’s all about how to convert datetime to date in Python.