Display datetime without Time Component in Python (Example Code)
This tutorial explains how to display a datetime object without the time component in Python programming.
Setting up the Example
First, we have to load the datetime module, as you can see below:
import datetime # Load datetime |
import datetime # Load datetime
Then we have to create a new datetime object.
x = datetime.datetime(2025, 7, 13, 11, 12, 55) # Constructing a datetime object print(x) # 2025-07-13 11:12:55 |
x = datetime.datetime(2025, 7, 13, 11, 12, 55) # Constructing a datetime object print(x) # 2025-07-13 11:12:55
Example: Applying date() Function to Delete Time of datetime
To get rid of the time part in our datetime object, we can use the date() function:
x_only_date = x.date() # Remove time print(x_only_date) # 2025-07-13 |
x_only_date = x.date() # Remove time print(x_only_date) # 2025-07-13
As demonstrated by the code above, the output shows our example datetime object without the time component.
Further Resources
Please find some related tutorials below.
- Retain Only Date Part when Using pandas.to_datetime in Python
- Modify datetime Format in pandas DataFrame in Python (2 Examples)
- Transform datetime Object to Date & Vice Versa in Python (2 Examples)
- All Python Programming Tutorials
Note: This article was created in collaboration with Matthias Bäuerlen. Matthias is a programmer who helps to create tutorials on the Python programming language. You might find more info about Matthias and his other articles on his profile page.