How to check if a DateTime occurs today?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
To check whether a datetime occurs today, compare only the date portion, not the full timestamp. The important subtlety is timezone: a moment that is "today" in one timezone may already be "tomorrow" or "yesterday" in another.
Compare the Date Part Directly
In Python, the simplest case is to compare the date() of the value against today’s date:
This works for naive datetimes that already represent local time in the same timezone context as date.today().
Use the Same Timezone on Both Sides
If the datetime is timezone-aware, convert it to the timezone that defines "today" for your application:
Without the conversion, you may compare the right instant against the wrong calendar day.
Wrap the Logic in a Helper
If this rule appears in several places, a helper keeps the timezone choice explicit:
This is cleaner than repeating conversion and comparison logic throughout the codebase.
Avoid String-Based Comparisons
You might see code that formats both values as strings and compares them. That works only incidentally and makes timezone reasoning less clear:
Comparing date objects directly is simpler and less error-prone.
Test Boundary Cases Explicitly
Date logic often fails around midnight, daylight-saving transitions, and timezone conversions. A good unit test should include values just before and just after midnight in the target timezone:
Testing these boundaries is often more valuable than testing random midday timestamps.
If the source timestamps arrive in UTC, convert them before applying "today" logic. That keeps the business rule tied to the intended calendar, not to the transport format.
This is a good place to write tests against a fixed timezone rather than relying on whatever timezone the CI server happens to use.
Doing that makes date-sensitive bugs reproducible instead of intermittent.
It also documents what "today" means in the application.
That clarity prevents subtle support issues later.
Common Pitfalls
The biggest mistake is comparing a timezone-aware datetime with a naive "today" value. That can shift the calendar day unexpectedly.
Another issue is comparing full datetime objects instead of their date parts. A timestamp at 09:00 today is not equal to datetime.now(), but it still occurs today.
People also forget that server-local time and user-local time are often different. If "today" is user-facing, use the user’s timezone rather than the server default.
Finally, if the input might already be a date object instead of a datetime, handle that consistently instead of calling .date() blindly.
Summary
- Compare the date portion of the datetime against today’s date.
- Use the same timezone on both sides of the comparison.
- Prefer direct
datecomparisons over string formatting tricks. - Put the logic in a helper if the rule appears in many places.
- Decide whether "today" means server-local time or user-local time before writing the check.
.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.