replace(): create a new date or datetime object by replacing specified attributes (such as year, month, or day) with new values


replace() : Update date object by changing the parameters with new value .
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

Using date object and adding new time part

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

Replacing month with invalid date.

This code will generate ValueError
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


Example 1: Adjusting Time While Keeping the Date Constant

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

Example 2: Setting a Specific Date for Recurring Events

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

Example 3: Handling End-of-Month Dates

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

Example 4: Creating a Series of Dates with Modified Attributes

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

Example 5: Combining `replace()` with Timezone Information

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

All Date Objects
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer