How To Modify Datetime Attributes

Published on June 18, 2022 | Tags: python datetime TIL

When working with datetime objects in python if you want to change any of their
attributes you can do that with the .replace() method:


>>> from datetime import datetime
>>> now = datetime.now()
>>> new_now = now.replace(hour=2)
>>> print(new_now.time().hour)
2


Ferran Jovell