Wednesday, January 22, 2025
HomeConversionsHow to convert datetime to date in Python

How to convert datetime to date in Python

In Python, you can convert a datetime object to a date object by using the .date() method. Here’s an example:

from datetime import datetime

# Sample datetime object
dt = datetime(2025, 1, 22, 15, 30, 45)

# Convert datetime to date
date_only = dt.date()

print(date_only)

Output:

2025-01-22

The .date() method will extract the date part (year, month, and day) from the datetime object and return it as a date object.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x