10

I want to add two datetime objects.

>>> from datetime import datetime
>>> a = datetime.strptime("04:30",'%H:%M')
>>> b = datetime.strptime("02:30",'%H:%M')
>>> a
datetime.datetime(1900, 1, 1, 4, 30)
>>> b
datetime.datetime(1900, 1, 1, 2, 30)

when i subtract b from a, it gives me the output

>>> a-b
datetime.timedelta(0, 7200)

but, when i add a and b it gives me error

>>> a+b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
 TypeError: unsupported operand type(s) for +: 'datetime.datetime' and    'datetime.datetime'

I want to add the time of b to time of a, i.e i want this.

datetime.datetime(1900, 1, 1, 7, 00)

please help ?

2
  • 1
    Adding dates does not make sense. There is no logical reason to expect the output you want (why should the hours and minutes be added but not the years, months and days?) Commented Mar 11, 2015 at 13:47
  • 2
    While adding dates does not make sense, what you want is probably to add a time difference to a time. Therefore, you should make your b not a datetime object, but a timedelta object. Commented Mar 11, 2015 at 13:50

2 Answers 2

7

Agreeing with the previous poster, there isn't a meaningful way to add two datetimes, as they're just points in time, you can only deal with the difference between them (timedeltas). Since you don't explicitly mention the dates in your example, this seems like it would be more along the lines of what you're trying to accomplish:

>>> a = datetime.timedelta(0, (4*3600+30*60))
>>> b = datetime.timedelta(0, (2*3600+30*60))
>>> a+b
datetime.timedelta(0, 25200)
>>> print a+b
7:00:00

As timedeltas take days, seconds, and microseconds you need to multiply your hours and minutes to get them to the correct base.

Sign up to request clarification or add additional context in comments.

Comments

1

We recently came across a similar scenario where we were provide two datetime values, one containing just a date (but no time) and another containing just a time.

Here's the solution we ended up with in case it helps anyone else in future:

from datetime import datetime, timedelta

BASE_DATE = datetime(1900, 1, 1)

def sumDateTimes(dates):
    return sum((dt - BASE_DATE for dt in dates), timedelta(0)) + BASE_DATE

Example:

a = datetime.strptime("1-Aug-23", "%d-%b-%y")
b = datetime.strptime("10 AM", "%I %p")
print(sumDateTimes([ a, b ]))
>>> 2023-08-01 10:00:00

2 Comments

datetime.combine is the most natural way of solving this requirement: docs.python.org/3/library/…. Specifically: print(datetime.combine(a, b.time())) will give the required "2023-08-01 10:00:00".
@slothrop datetime.combine would work for this particular scenario, however the above would work regardless of which order the dates were specified and would additionally handle multiple time offsets or even offsets of larger than a day.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.