ADVERTISEMENT
ADVERTISEMENT

Python date replace() Method with Example

Python date.replace() Method: Here, we are going to learn about the replace() method of date class in Python with its definition, syntax, and examples.
Submitted by Hritika Rajput, on April 29, 2020

Python date.replace() Method

date.replace() method is used to manipulate objects of date class of module datetime.

It 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:

    import datetime

Class:

    from datetime import date

Syntax:

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

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:

## 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:

## 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

ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.