from datetime import datetime
dt = datetime(2019, 12, 31)
print(dt.replace(year=2018, day=14, month=7))
Output
2018-07-14 00:00:00
Date with time
import datetime
# Year, Month, date, Hour, Minute, Second, microsecond
dt = datetime.datetime(2019, 12, 31, 23, 59, 59, 345234)
print(dt.replace(day=14))
Output
2019-12-14 23:59:59.345234
from datetime import datetime
dt = datetime(2019, 12, 31)
print(dt.replace(year=2018, day=14, month=7,
hour=13, minute=35, second=55,
microsecond=324356))
Output
2018-07-14 13:35:55.324356
All parameters are optional
from datetime import datetime
dt = datetime(2019, 12, 31)
print(dt.replace())
Output
2019-12-31 00:00:00
from datetime import datetime
dt = datetime(2020, 3, 31)
dt = dt.replace(month=4)
print(dt)
Output
ValueError: day is out of range for month
We can use Exception Handling to manage the code in this case.
from datetime import datetime
try:
dt = datetime(2020, 3, 31)
dt = dt.replace(month=4)
print(dt)
except ValueError as my_msg:
print("This is a ValueError")
print(my_msg)
Output is here
This is a ValueError
day is out of range for month
from datetime import datetime
# Original datetime object
original_dt = datetime(2025, 2, 8, 15, 30, 45)
print("Original datetime:", original_dt)
# Replace hour, minute, and second
new_dt = original_dt.replace(hour=10, minute=0, second=0)
print("Updated datetime:", new_dt)
Output
Original datetime: 2025-02-08 15:30:45
Updated datetime: 2025-02-08 10:00:00
from datetime import datetime
# Original datetime object
original_dt = datetime(2025, 2, 8, 15, 30, 45)
print("Original datetime:", original_dt)
# Replace day to 15th
event_dt = original_dt.replace(day=15)
print("Event datetime:", event_dt)
Output
Original datetime: 2025-02-08 15:30:45
Event datetime: 2025-02-15 15:30:45
from datetime import datetime
# Original datetime object on March 31st
original_dt = datetime(2025, 3, 31)
print("Original datetime:", original_dt)
# Attempt to replace month to February
try:
new_dt = original_dt.replace(month=2)
print("Updated datetime:", new_dt)
except ValueError as e:
print("Error:", e)
Output
Original datetime: 2025-03-31 00:00:00
Error: day is out of range for month
from datetime import datetime, timedelta
# Original datetime object
start_dt = datetime(2025, 1, 1)
print("Start datetime:", start_dt)
# Generate dates with the same time on consecutive days
dates = [start_dt.replace(day=day) for day in range(1, 6)]
for dt in dates:
print(dt)
Output
Start datetime: 2025-01-01 00:00:00
2025-01-01 00:00:00
2025-01-02 00:00:00
2025-01-03 00:00:00
2025-01-04 00:00:00
2025-01-05 00:00:00
from datetime import datetime, timezone, timedelta
# Original naive datetime object
naive_dt = datetime(2025, 2, 8, 15, 30, 45)
print("Naive datetime:", naive_dt)
# Define UTC+5 timezone
utc_plus_5 = timezone(timedelta(hours=5))
# Replace timezone
aware_dt = naive_dt.replace(tzinfo=utc_plus_5)
print("Timezone-aware datetime:", aware_dt)
Output
Naive datetime: 2025-02-08 15:30:45
Timezone-aware datetime: 2025-02-08 15:30:45+05:00
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.