[英]Python datetime.date issue TypeError: an integer is required
I am have a problem that I need help with, I have the following python script and trying figure pass values to datetime.date.我有一个需要帮助的问题,我有以下 python 脚本并尝试将数字传递值传递给 datetime.date。
EXPIRE_DATE = ("{}, {}, {}".format(YEAR,MONTH_NUMBER,DAY)).strip()
This returns 2023, 6, 14
这将返回
2023, 6, 14
Now, I want to pass "EXPIRE_DATE"
to datetime.date
.现在,我想将
"EXPIRE_DATE"
传递给datetime.date
。 But, I am getting an error:但是,我收到一个错误:
today = datetime.date.today()
someday = datetime.date(EXPIRE_DATE)
Error:
TypeError: an integer is required
Your code creates a string while datetime.date() requires three integers as parameters (year, month, day).您的代码创建一个字符串,而datetime.date()需要三个整数作为参数(年、月、日)。
EXPIRE_DATE = ("{}, {}, {}" .format(YEAR,MONTH_NUMBER,DAY)).strip()
Instead you can just pass the year, month and day to the function directly, like such;相反,您可以直接将年、月和日传递给 function,就像这样;
YEAR = 2000
MONTH_NUMBER = 10
DAY = 30
today = datetime.date.today()
someday = datetime.date(YEAR, MONTH_NUMBER, DAY)
print(someday) // Prints '2000-10-30'
Your EXPIRE_DATE
is a String
and as the error correctly says you need three Integers
as arguments for datetime.date
.您的
EXPIRE_DATE
是一个String
,并且错误正确地表明您需要三个Integers
作为datetime.date
的 arguments 。 Specifically, something like:具体来说,类似:
YEAR = 2021
MONTH = 3
DAY = 17
today = datetime.date.today()
someday = datetime.date(YEAR, MONTH, DAY)
Further reading on datetime.date
: https://docs.python.org/3/library/datetime.html#datetime.date进一步阅读
datetime.date
: https://docs.python.org/3/library/datetime.html#datetime.date
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.