1

I am trying to run this code:

if initial_hour_obj.hour == 8:
    print (initial_hour_obj)
    initial_hour_obj.replace(hour = 10)
    print (initial_hour_obj)

2022-03-23 08:30:00
2022-03-23 08:30:00

but the replace method wont work. If I try to print a message after the if statement to see if my datetime obj hour is equal to the integer 8, it prints my message, so the evaluation is working, and if I try the .replace() method outside of the if, it also works, any ideas why they wont work together?

thanks

2
  • 1
    Replace() returns the result of said replacing, which you arent saving anywhere. Commented Mar 23, 2022 at 9:03
  • Thanks, got it now, initial_hour_obj = initial_hour_obj.replace(hour = 10) would have the effect I need Commented Mar 23, 2022 at 9:06

1 Answer 1

2

When you are running replace(), it is returning the result, it is not storing the result in initial_hour_obj.

So, you have to do this:

if initial_hour_obj.hour == 8:
    print (initial_hour_obj)
    initial_hour_obj = initial_hour_obj.replace(hour = 10)
    print (initial_hour_obj)
Sign up to request clarification or add additional context in comments.

Comments

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.