How to truncate the time on a datetime object?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Truncating the time on a datetime object means removing the time component (hours, minutes, seconds, microseconds) and keeping only the date, effectively setting the time to midnight (00:00:00). This is useful for date-only comparisons, grouping records by day, and generating daily reports. Python offers several approaches: the .date() method (returns a date object), the .replace() method (returns a datetime at midnight), and datetime.combine(). Other languages have similar patterns.
Python: Using .date()
Note: .date() returns a date object, not a datetime. If you need a datetime at midnight, use .replace() or combine().
Python: Using .replace()
Python: Using datetime.combine()
Truncating to Different Precisions
Pandas: Truncating DateTime Columns
JavaScript
C#
Common Pitfalls
- Using
.date()when adatetimeis needed: Python's.date()returns adateobject which cannot be compared directly todatetimeobjects or used in datetime arithmetic (timedelta). Use.replace(hour=0, minute=0, second=0, microsecond=0)if you need adatetimeat midnight. - Time zone issues when truncating: Truncating a UTC datetime and a local datetime to the same date may produce different dates near midnight.
2025-03-15T23:30:00 UTCis2025-03-16in UTC+2. Always truncate in the correct time zone context. - JavaScript local vs UTC time:
setHours(0,0,0,0)uses local time, whiletoISOString()returns UTC. Truncating in local time and then comparing to UTC timestamps gives wrong results near midnight. UsesetUTCHours(0,0,0,0)for UTC truncation. - Pandas
.dt.datereturning Python objects:df['col'].dt.datereturns Pythondateobjects, not pandasTimestamp. These cannot be used with pandas datetime operations. Use.dt.normalize()to get midnightTimestampvalues instead. - Losing timezone info with
.replace(): In Python,.replace()on a timezone-aware datetime preserves the timezone. However,datetime.combine(dt.date(), time.min)creates a naive datetime (no timezone) even if the original was timezone-aware. Pass thetzinfoargument:datetime.combine(dt.date(), time.min, tzinfo=dt.tzinfo).
Summary
- Python: Use
.date()for adateobject,.replace(hour=0, ...)for adatetimeat midnight - Use
datetime.combine(dt.date(), time.min)as an alternative - Pandas: Use
.dt.normalize()for midnight or.dt.floor('h')for truncating to hour - JavaScript: Use
setHours(0,0,0,0)for local time truncation - C#: Use
.Dateproperty orDateOnly.FromDateTime() - Always consider timezone context when truncating near midnight
Related reading
- How to tune parameters in Random Forest, using Scikit Learn?
- How to type hint a generator in Python 3?
- How to understand sess.as_default and sess.graph.as_default?
- How to understand tf.get_collection in TensorFlow
- How to unimport a python module which is already imported?
- How to uninstall a package installed with pip install --user
- How to uninstall Python 2.7 on a Mac OS X 10.6.4?
- How to unnest explode a column in a pandas DataFrame, into multiple rows
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.