Compute the DateTime of an upcoming weekday
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Computing the next occurrence of a weekday is a small problem that shows up everywhere: reminders, reports, calendar logic, and scheduling tools. The main trick is deciding whether "upcoming" includes today and then using modular arithmetic so the calculation works cleanly across week boundaries.
Work With weekday() Numbers
In Python, datetime.weekday() returns an integer from 0 to 6:
- '
0means Monday' - '
6means Sunday'
If you know today's weekday number and the target weekday number, the offset in days is:
That handles wrap-around automatically. For example, if today is Friday, represented by 4, and the target is Monday, represented by 0, the result is (0 - 4) % 7, which is 3.
A Reusable Function
Here is a practical function that works with either date or datetime values and lets you choose whether today counts.
If now is already Monday and include_today is False, the function returns the Monday of the following week. If include_today is True, it returns the same day.
That is the most important business-rule decision in this problem.
Preserve Time or Reset It Intentionally
When you add a day offset to a datetime, Python preserves the time component. That is often what you want.
For example, if the input is Wednesday at 14:30, the next Monday returned by the function is also at 14:30.
If you instead want the start of that day, reset the time explicitly.
Be explicit about that requirement. Some scheduling bugs come from preserving the input time when the application actually wanted midnight or a fixed scheduled hour.
Named Weekdays Improve Readability
Hard-coding weekday numbers works, but named constants are easier to read.
This makes business rules clearer when you revisit the code later.
Time Zone Awareness Still Matters
The arithmetic itself is simple, but time zones can complicate the surrounding logic. If the input datetime is timezone-aware, the returned value stays aware because you are adding a timedelta to the original object.
That is usually correct, but do not ignore what time zone the input actually represents. "Next Monday at 9:00" in one zone may already be Tuesday somewhere else.
For applications involving users in multiple regions, make the time zone part of the design rather than an afterthought.
Common Pitfalls
The most common mistake is forgetting to define whether the current day counts as upcoming. That leads to off-by-one-week bugs that only appear on one weekday.
Another mistake is mixing up Python's weekday numbering and assuming Sunday is 0.
A third issue is using date logic when the application really needs datetime behavior with preserved time or time-zone awareness.
Finally, do not write long if chains for each weekday. Modular arithmetic is shorter, clearer, and less error-prone.
Summary
- Use
weekday()and modular arithmetic to compute the next target day. - Decide explicitly whether "upcoming" includes today.
- Adding a
timedeltapreserves the time component of adatetime. - Reset the time manually if the result should be midnight or a fixed hour.
- Named weekday constants improve readability.
- Time zone handling matters whenever the datetime crosses user or system boundaries.
Related reading
- Computing generalized eigen values for sparse matrices in python
- Comsuming MassTransit from Python or other languages
- Concatenate a list of pandas dataframes together
- Concatenate a NumPy array to another NumPy array
- Concatenate strings from several rows using Pandas groupby
- Concatenating two lists - difference between '' and extend
- Concatenating two one-dimensional NumPy arrays
- Concept of getter in TensorFlow
.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.