Python datetime replace() Method with Example

Python datetime.replace() Method: In this tutorial, we will learn about the replace() method of datetime class in Python with its usage, syntax, and examples. By Hritika Rajput Last updated : April 22, 2023

Python datetime.replace() Method

The datetime.replace() method is used to replace the date and time with the same value, except for those parameters given new values by whichever keyword arguments are specified in the brackets. It is an instance method which means that it works on an instance of the class.

Module

The following module is required to use replace() method:

import datetime

Class

The following class is required to use replace() method:

from datetime import datetime

Syntax

The following is the syntax of replace() method:

replace(
    year=self.year, 
    month=self.month, 
    day=self.day, 
    hour=self.hour, 
    minute=self.minute, 
    second=self.second, 
    microsecond=self.microsecond, 
    tzinfo=self.tzinfo, 
    * fold=0)

Parameter(s)

The following are the parameter(s):

  • year: new year value of the instance (range: 1 <= year <= 9999)
  • month: new month value of the instance (range: 1 <= month <= 12)
  • day: new day of the instance (range: 1<= day <= 31)
  • hour: in range(24)
  • minute: in range(60)
  • second: in range(60)
  • microsecond: in range(1000000)
  • tzinfo: object passed as the tzinfo argument to the datetime constructor, or None if none was passed.
  • fold: [0,1]

Return Value

The return type of this method is a datetime class object after replacing the parameters.

If values are not in the given range a ValueError is raised.

Example of datetime replace() Method in Python

## Python program explaining the 
## use of datetime class instance methods

from datetime import datetime
import pytz

## Creating an instance
x = datetime(2019, 9, 25,4,54,23)
print("Datetime entered was:", x)
print()

x = datetime.now()
print("Today's date and time:", x)

## Using replace() method 
d = x.replace(year = 2022)
print("New date after changing the year:", d)
print()

d = x.replace(month=1)
print("The date after changing the month:", d)
print()

d = x.replace(day=3)
print("The date after changing the day:", d)
print()

d = x.replace(year=2025, day=30)
print("The date after changing the day and year:", d)
print()

d = x.replace(year= 1999, month =12, day=3)
print("The date after changing the year, month and day:", d)
print()

d = x.replace(hour = 12)
print("The date after changing the hour:",d)
print()

d = x.replace(minute= 4)
print("The date after changing the minute attribute:",d)
print()

d = x.replace(year=2220, month=10, day=28, hour=21, minute =5, second = 20)
print("The date after the changes:",d)
print()

timezone = pytz.timezone("Asia/Kolkata")
d = x.replace(tzinfo=timezone)
print("The date after changing the tzinfo:",d)

Output

Datetime entered was: 2019-09-25 04:54:23

Today's date and time: 2020-04-30 19:11:10.683769
New date after changing the year: 2022-04-30 19:11:10.683769

The date after changing the month: 2020-01-30 19:11:10.683769

The date after changing the day: 2020-04-03 19:11:10.683769
The date after changing the day and year: 2025-04-30 19:11:
10.683769
The date after changing the year, month and day: 1999-12-03
 19:11:10.683769

The date after changing the hour: 2020-04-30 12:11:10.683769

The date after changing the minute attribute: 2020-04-30 19
:04:10.683769

The date after the changes: 2220-10-28 21:05:20.683769

The date after changing the tzinfo: 2020-04-30 19:11:10.683
769+05:53



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.