Python date replace() Method with Example

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

Python date.replace() Method

The date.replace() method is used to replace the date 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 date

Syntax

The following is the syntax of replace() method:

replace(year=self.year, month=self.month, day=self.day)

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)

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

Return Value

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

Example of date replace() Method in Python

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

from datetime import date

## Creating an instance
x = date(2019, 9, 25)
print("Current date is:", x)
print()

## Using replace() method 
d = x.replace(year = 2020)
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=30)
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()

Output

Current date is: 2019-09-25

New date after changing the year: 2020-09-25

The date after changing the month: 2019-01-25

The date after changing the day: 2019-09-30

The date after changing the day and year: 2025-09-30

The date after changing the year, month and day: 1999-12-03

Note:

The method will show an error if any of the parameters are out of range or the new date is invalid. For example, February has 28( or 29) days so if you enter 29 for a non-leap year, it will show a ValueError.

Example 2

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

from datetime import date

## Creating an instance
x = date(2019, 9, 25)
print("Current date is:", x)
print()

d = x.replace(year = 2020, month =2, day =29)
print("New date:",d)
print()

d = x.replace(year = 2019, month =2, day =29)
print(d)

Output

Current date is: 2019-09-25

New date: 2020-02-29

Traceback (most recent call last):
  File "main.py", line 15, in <module>
    d = x.replace(year = 2019, month =2, day =29)
ValueError: day is out of range for month

Runtime Error(s):

ValueError: day is out of range for month

Comments and Discussions!

Load comments ↻





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