0

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
2
  • note: 2023, 6, 14 (result of "{}, {}, {}" .format) is a string Commented Mar 17, 2021 at 15:24
  • What's the reason for turning your year, month, and date values into a string before trying to create the date? There are ways to build a date from a string (docs.python.org/3/library/… and then use .date() to take just the date portion, or third party libraries like the excellent dateutil) but since you appear to have integers to start with they shouldn't be necessary. Or are YEAR etc themselves strings? Commented Mar 17, 2021 at 15:24

2 Answers 2

1

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'

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.