简体   繁体   中英

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.

EXPIRE_DATE = ("{}, {}, {}".format(YEAR,MONTH_NUMBER,DAY)).strip()

This returns 2023, 6, 14

Now, I want to pass "EXPIRE_DATE" to 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).

EXPIRE_DATE = ("{}, {}, {}" .format(YEAR,MONTH_NUMBER,DAY)).strip()

Instead you can just pass the year, month and day to the function directly, like such;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM