. Advertisement .
..3..
. Advertisement .
..4..
Wondering how to convert datetime to date in Python? This instruction is aimed to help with the most detailed explanation. Let’s jump right in!
What Are Datetime And Date Objects?
Little did you know, Python doesn’t have a built-in data type specifically for storing time and date. But it does have a datetime module that gives the user access to classes letting them change and manage the date and time in Python code.
So simply putting, datetime is an object that we employ to return the date and time. It is frequently a combination of a wide range of values, including the second, microsecond, hour, minute, day, year and month.
What about the date object? On the contrary of such a bountiful collection contained in datetime, the date object encloses the date only.
How To Convert Python Datetime To Date?
Method #1: Employ Pandas
In creating arrays that can hold time and date information, employing the Pandas DataFrame is also one of the most acchievable approaches you may not want to miss out on.
We must first import the pandas module in order to utilize the pandas DataFrame and create an array using the time column.
Running the code:
import pandas as pd
dataf = pd.DataFrame({'EMP': [3, 6],'time': ['2021-10-15 16:05:00','2021-10-17 20:00:30']})
print(dataf)
dataf['time'] = pd.to_datetime(dataf['time']).dt.date
print(dataf)
Output:
EMP time
0 3 2021-10-15 16:05:00
1 6 2021-10-17 20:00:30
EMP time
0 3 2021-10-15
1 6 2021-10-17
The time column in the code above only shows the time and the date following the dt.date function’s execution, as is evident.
Method #2: Utilize The datetime.date() Function
- The date() function
As you can see in the code below, the day, month, and year are the three inputs for the datetime()
constructor. To produce a date using the constructor, these three arguments are vastly necessary.
Simply using the date()
method, which returns an object of the date type, and there you go to have your problem solved! Check out the following example for further understanding.
Running the code:
# 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
So first thing first is to import the datetime module. Then the next step forward is to employ the datetime.datetime.now()
method to generate the current time and date in the form of a datetime object.
That way, the date()
function and the datetime.datetime.now()
function both provide the current date as an object of the type date.
- The datetime.date.today() Function
Similarly, the datetime.date.today()
method can also be of great use if you want to directly find the current date. However, if we require anything other than the current date, this solution will be ineffective.
The function datetime.date.today()
is used in the code that follows.
Running the code:
import datetime
print(datetime.datetime.now())
print(datetime.date.today())
Output:
2021-10-17 21:27:46.018327
2021-10-17
Given that we are working with the current date in this specific instance, both strategies get the same results.
Conclusion
Above is neatly all that you may grasp about how to convert datetime to date in Python. We hope this post could be much workable to your code. See then!
Leave a comment