Convert datetime to date in Python

Convert datetime to date in Python

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.

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

The above code provides the following output:

2021-06-19 23:27:13.138679 ,type:
2021-06-19 ,type:

Explanation

  • The datetime module is imported to work with date and time data types.
  • The datetime.datetime.now() is used to provide the current date and time as a datetime object.
  • The date() method is used on datetimeObj to 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()

The above code provides the following output:

2021-06-19 00:34:05.732545
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.

The above code provides the following output:

2021-06-19 00:00:00 ,type:
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.

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.

The above code provides the following output:

employee time
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 DataFrame is created and the datetime values are added to a column.
  • Now, the dt.date() function is used on the time column in the recently created DataFrame to convert datetime value to a date value.
  • Both the datetime value and the date value is returned in the output using the print command.

That’s all about how to convert datetime to date in Python.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *