Date format issue in print command (beginner - very basic)

So I am at my last assignment for the weekend. The assignment was to write a program that would add a number of hours to the current time and show when the alarm would go off. The program does what it should do, but the format the time is printed bothers me. I want it to be the same format as in my first line. I have tried to use “strftime” in the print command, but this gives an error message.

INPUT
import datetime
from datetime import timedelta
now = datetime.datetime.now().strftime(’%d-%m, %Hu%M’)
print(“It is now”, now)
alarm = int(input("In how many hours do you want the alarm to go off? "))
print(“Thanks,”)
print(“The alarm will go off in”, alarm, “hours.”)
print("It will then be " + str(datetime.datetime.now() + timedelta(hours=alarm)))

OUTPUT
It is now 31-01, 10u55
In how many hours do you want the alarm to go off? 3
Thanks
The alarm will go off in 3 hours.
It will then be 2021-01-31 13:55:36.666980

Any suggestion on how to solve this would be greatly appreciated!

It would help to see your best try with strftime and the full error
message. Using strftime is what I would do.

Remember that strftime is a method of the datetme.datetime class. So
your first line:

now = datetime.datetime.now().strftime('%d-%m, %Hu%M')

Gets a datetime.datetime:

datetime.datetime.now()

and calls the .strftime method on that.

Your last line goes:

print("It will then be " + str(datetime.datetime.now() + timedelta(hours=alarm)))

Let’s make that less complicated:

print("It will then be", datetime.datetime.now() + timedelta(hours=alarm))

because print() calls str() on all of its arguments anyway. And str() is
where the ISO8601 format comes from, because that’s what the
datetime.datetime.str method does.

What did you do with strftime? Remember that it is a method of the
datetime.datetime class. What are the types of your expressions?

datetime.datetime.now()     # a datetime
timedelta(hours=alarm))     # a timedelta
datetime.datetime.now() + timedelta(hours=alarm))     # also a datetime

Where did you put your .strftime()? What was the type of the thing your
used it on? Might you have used it on a timedelta instead?

The “.” operator binds more tightly than “+”. What do you do when
precedence makes things bind not how you want? Consider this:

2 + 3 * 5

That produces 17. Why 17 instead of 25? What if we wanted 25? How would
you change the above expression to do that?

Having fixed that, how might you change how you’re using .strftime? (I’m
guessing about what you did here, since you’ve not shown us the code
giving you the error from strftime.)

FInally, it is an acknowledged unfortunateness that the “datetime”
module has the same name as the “datetime” class. Most of us go:

from datetime import datetime, timedelta

and you can stop saying “datetime.datetime”, instead just saying
“datetime”.

Cheers,
Cameron Simpson cs@cskk.id.au

Great, it was indeed an issue of using the “strftime” on timedelta that was causing the issue. Thanks for pointing me in the right direction. The program is now printing exactly what I want ;-).

This is how it looks now, all cleaned up…
from datetime import datetime, timedelta
now = datetime.now().strftime(’%d-%m, %Hu%M’)
print(“It is now”, now)
alarm = int(input("In how many hours do you want the alarm to go off? "))
print(“Thanks,”)
print(“The alarm will go off in”, alarm, “hours.”)
print(“It will then be”, (datetime.now() + timedelta(hours=alarm)).strftime(’%d-%m, %Hu%M’))