python 示例
Python date.replace()方法 (Python date.replace() Method)
date.replace() method is used to manipulate objects of date class of module datetime.
date.replace()方法用于操作datetime模块的date类的对象。
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)
year :实例的新年份值(范围:1 <= year <= 9999)
month: new month value of the instance (range: 1 <= month <= 12)
month :实例的新月份值(范围:1 <= month <= 12)
day: new day of the instance (range: 1<= day <= 31)
day :实例的新日期(范围:1 <= day <= 31)
If values are not in the given range a ValueError is raised.
如果值不在给定范围内,则会引发ValueError 。
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.
如果任何参数超出范围或新日期无效,则该方法将显示错误。 例如,二月有28(或29)天,因此如果您输入29表示非-年,它将显示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
ValueError:日期超出月份范围
翻译自: https://www.includehelp.com/python/date-replace-method-with-example.aspx
python 示例