datetime
programming
weekday calculations
coding tutorial
Python

Compute the DateTime of an upcoming weekday

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

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:

  • '0 means Monday'
  • '6 means Sunday'

If you know today's weekday number and the target weekday number, the offset in days is:

python
(target - current) % 7

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.

python
1from datetime import datetime, timedelta
2
3
4def upcoming_weekday(dt, target_weekday, include_today=False):
5    days_ahead = (target_weekday - dt.weekday()) % 7
6
7    if days_ahead == 0 and not include_today:
8        days_ahead = 7
9
10    return dt + timedelta(days=days_ahead)
11
12
13now = datetime(2026, 3, 11, 14, 30)  # Wednesday
14next_monday = upcoming_weekday(now, 0)
15print(next_monday)

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.

python
1from datetime import datetime, time
2
3
4def next_weekday_at_midnight(dt, target_weekday):
5    result = upcoming_weekday(dt, target_weekday)
6    return datetime.combine(result.date(), time.min, tzinfo=result.tzinfo)

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.

python
1MONDAY = 0
2TUESDAY = 1
3WEDNESDAY = 2
4THURSDAY = 3
5FRIDAY = 4
6SATURDAY = 5
7SUNDAY = 6
8
9print(upcoming_weekday(datetime.now(), FRIDAY))

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 timedelta preserves the time component of a datetime.
  • 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.

Course illustration
Course illustration

All Rights Reserved.