Python datetime Subtract Minutes: timedelta Explained
To subtract minutes from a Python date or time, subtract a datetime.timedelta: for example, new_time = old_time - timedelta(minutes=15). This works with datetime.datetime and datetime.time only indirectly: a datetime.time has no date or arithmetic support, so combine it with a date or calculate the minutes another way.
Basic datetime subtraction
Python’s standard datetime module represents a span of time with timedelta. Pass the number of minutes as the minutes argument, then subtract the resulting duration from a datetime value:
from datetime import datetime, timedelta
start = datetime(2025, 5, 20, 14, 30)
result = start - timedelta(minutes=15)
print(result) # 2025-05-20 14:15:00
The calculation preserves the date and other fields as needed. If the subtraction crosses midnight, Python adjusts the date as well:
from datetime import datetime, timedelta
start = datetime(2025, 5, 20, 0, 10)
result = start - timedelta(minutes=20)
print(result) # 2025-05-19 23:50:00
What timedelta represents
A timedelta is a duration, not a particular clock reading. It stores a normalized combination of days, seconds, and microseconds. The constructor also accepts convenient keyword arguments including weeks, days, hours, minutes, seconds, and milliseconds.
For example, subtracting 90 minutes is straightforward: moment - timedelta(minutes=90). You do not need to convert 90 minutes into an hour and 30 minutes yourself. Multiple units can be combined, too: timedelta(hours=1, minutes=30). When a timedelta is created, Python normalizes these values, so equivalent durations can be expressed in different ways.
Use keyword arguments for readability. The positional form of timedelta assigns its first argument to days, not minutes; timedelta(15) means 15 days. Writing timedelta(minutes=15) makes the intended unit explicit and avoids a common error.
Subtracting from datetime and date
Subtraction works with a datetime.datetime because it includes both a date and a time. It also works with a datetime.date, but a date has no time-of-day component. A date minus a duration is represented as a date, with sub-day parts unable to appear in the result. For minute-level work, use a datetime if you need to retain the clock time.
Python also supports subtracting one datetime from another. That operation returns a timedelta, which is useful for measuring elapsed time:
from datetime import datetime
later = datetime(2025, 5, 20, 14, 30)
earlier = datetime(2025, 5, 20, 14, 10)
elapsed = later - earlier
print(elapsed) # 0:20:00
To express that duration as a number of minutes, use elapsed.total_seconds() / 60. Avoid using elapsed.seconds / 60 when the duration may include whole days: the seconds attribute is only the seconds portion remaining after days are accounted for. total_seconds() includes the full duration.
Working with time zones
If a datetime represents a real moment in a time zone, create an aware datetime and use a matching aware duration calculation. An aware datetime has a non-None tzinfo; a naive one has no time-zone information. Mixing an aware datetime and a naive datetime in comparisons or subtraction raises TypeError.
For example, a UTC timestamp can be made with datetime.now(timezone.utc). Subtracting 15 minutes from that value with timedelta(minutes=15) produces another UTC-aware datetime. For local civil times, daylight-saving transitions can make clock arithmetic surprising: subtracting a duration is not always the same as subtracting wall-clock labels. If the requirement concerns elapsed time, perform the calculation on timezone-aware instants, commonly by converting to UTC first. If it concerns a local scheduled time, handle the relevant time zone and daylight-saving rules deliberately.
Why datetime.time is different
A datetime.time object stores a time of day, such as 14:30, but not a date. It cannot be directly subtracted from a timedelta. That is because the result could cross midnight, and without a date there is no way to say whether the new value belongs to the previous or next day.
When day rollover matters, combine the time with a date and subtract from the resulting datetime:
from datetime import date, datetime, time, timedelta
clock_time = time(0, 10)
combined = datetime.combine(date(2025, 5, 20), clock_time)
result = combined - timedelta(minutes=20)
print(result) # 2025-05-19 23:50:00
If you only need arithmetic within a repeating 24-hour clock and do not care about the date, convert the time to minutes after midnight, subtract the requested minutes, and apply modulo 1440. Keep in mind that this intentionally discards whether the calculation crossed into another day.
Common mistakes and useful patterns
- Using the wrong constructor argument:
timedelta(5)is five days. Usetimedelta(minutes=5)for five minutes. - Confusing duration with clock time:
timedeltadescribes an amount of time;datetimeidentifies a date and time. - Ignoring midnight rollover: subtracting from a
datetimeautomatically adjusts the date. A standalonetimecannot capture that date change. - Formatting too early: keep values as datetime objects while calculating. Convert to text only for display, for example with
strftime. - Misreading elapsed seconds: use
total_seconds()to convert a duration that might span days, rather than relying on itssecondscomponent.
For repeated calculations, write a small function that accepts a datetime and a minute count. If the count is itself a decimal, timedelta(minutes=...) can represent fractional minutes to microsecond precision, subject to rounding in the conversion. For application data, also decide whether a negative minute count is valid: subtracting a negative duration moves the datetime forward.
Frequently Asked Questions
How do I subtract 5 minutes from the current time in Python?
Use from datetime import datetime, timedelta, then calculate datetime.now() - timedelta(minutes=5). For a time-zone-aware current timestamp, use datetime.now(timezone.utc) or another appropriate aware datetime.
Can I subtract minutes directly from a datetime?
No. Subtract a timedelta instead, such as my_datetime - timedelta(minutes=5). The datetime itself is not an integer number of minutes.
How do I subtract minutes from a Python time object?
A time object does not support duration arithmetic. Combine it with a date using datetime.combine, then subtract a timedelta, or convert it to minutes after midnight if you only need a 24-hour clock result.
What happens if subtracting minutes crosses midnight?
With a datetime, Python updates the date automatically. For example, subtracting 20 minutes from 00:10 gives 23:50 on the previous date.
How do I get the number of minutes between two datetimes?
Subtract the earlier datetime from the later one to get a timedelta, then divide total_seconds() by 60. Make sure both values are either naive or aware, and that their time-zone meaning is appropriate.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.