moment.date() returns a new date object with the same year, month, and day as the datetime. The hour, minute, second, microsecond, and tzinfo are dropped.
It is the usual way to compare a datetime with a date, to group timestamps by calendar day, or to print only the day. Its partners are time() for the clock part and combine() to put the two back together.
datetime.date() is an instance method of datetime.datetime: call it on an existing object with dot notation, such as moment.date(...). Import the class first, for example from datetime import datetime.
Returns datetime.date(2024, 1, 1): the zone is dropped.
datetime.now(timezone.utc).date()
Gives today's date in UTC, which can differ from date.today().
Do not confuse this method with the date class. After from datetime import date, datetime, date(2024, 3, 15) builds a date, while moment.date() extracts one.
Method signatures
python
datetime.date()
# returns: date with the same year, month, and day
Parameters
date() takes no parameters.
Return Value
Return Value
Description
date
The calendar day of the datetime, as seen in its own wall-clock fields.
Python Examples
Example 1 — Take the Date Part of a datetime
Extract the day from an evening timestamp and look at its type and repr.
python
from datetime import datetime
moment = datetime(2024, 3, 15, 18, 45, 10)
day = moment.date()
print(day)
print(type(day).__name__)
print(repr(day))
Output
2024-03-15
date
datetime.date(2024, 3, 15)
How It Works
Time removed. Only year, month, and day survive, so printing shows 2024-03-15.
New type. The result is a date, not a datetime at midnight.
Repr check.repr() confirms the three fields and the class name.
Example 2 — Count Events per Calendar Day
Use date() as a dictionary key to group timestamps that fall on the same day.
python
from datetime import datetime
events = [
datetime(2024, 3, 15, 9, 0),
datetime(2024, 3, 15, 17, 30),
datetime(2024, 3, 16, 8, 15),
]
counts = {}
for event in events:
counts[event.date()] = counts.get(event.date(), 0) + 1
for day, total in counts.items():
print(day.isoformat(), total)
Output
2024-03-15 2
2024-03-16 1
How It Works
Hashable keys.date objects are immutable and hashable, so they work as dictionary keys.
Same day, same key. 09:00 and 17:30 on 15 March both map to date(2024, 3, 15).
Insertion order. Dictionaries keep the order in which days first appeared, and isoformat() prints each key.
Example 3 — Get the Local Date and the UTC Date
At 08:00 in a UTC+11 zone it is still the previous day in UTC.