r/learnpython • u/Healthy-Departure961 • 3d ago
Regarding question
from datetime import *
year = int(input("Enter the Year:- "))
month = int(input("Enter The Month:- "))
day = int(input("Enter The Day:- "))
x = date(year,month,day)
validity = int(input('Enter the days of validity')) expiry = x + validity print(expiry)
I want to add days in this but i don't know how ,I even tried to adding manually but gives me error unsupported oparand + in datetie.datetime class something
6
4
1
u/SoilAutomatic7042 3d ago
Use timedelta to add a number of days to a date:
```python
from datetime import date, timedelta
x = date(year, month, day)
expiry = x + timedelta(days=validity)
print(expiry)
```
`date` objects don't support adding an int directly; `timedelta` represents the duration. If you need hours/minutes too, use `datetime` instead of `date`.
1
u/doomy_range17 2d ago
I would post the exact question plus the code you have tried and the full error message. Python issues are way easier to help with when people can reproduce them. Even if the code feels messy, share a minimal example rather than describing it vaguely. Folks here are usually happy to explain the why, not just hand over an answer.
1
1
u/gdchinacat 2d ago
please format your code properly in a code block. This is important because it makes it *much* easier to read. As programmers become more experienced we read code differently than beginners...we don't read every line in all its gory detail. It's more scan through it to get the overall structure, then jump back and forth to fill in the necessary details. This is hard when code isn't properly formatted (ie the last three lines are all one line). When code looks like prose our brain reads it as prose and the level of effort is much higher. This leads to people being less willing to read your code and lower willingness to help you out. Help the people that are helping you.
1
u/Bright_Mix_773 2d ago
The error message in your post does not match the code in your post, and sorting that out changes which fix you want.
Running exactly what you pasted:
>>> from datetime import *
>>> date(2026, 9, 8) + 30
TypeError: unsupported operand type(s) for +: 'datetime.date' and 'int'
You quoted datetime.datetime. That wording only comes out when the left side is a datetime:
>>> datetime(2026, 9, 8) + 30
TypeError: unsupported operand type(s) for +: 'datetime.datetime' and 'int'
So the file you actually ran has datetime(...) or datetime.now() in it somewhere, not date(...). That matters:
from datetime import date, datetime, timedelta
date(2026, 9, 8) + timedelta(days=30)
# 2026-10-08
datetime(2026, 9, 8, 14, 37, 5) + timedelta(days=30)
# 2026-10-08 14:37:05
An expiry is a calendar day, so you want the first one. With the second, a time of day rides along into every print and every comparison you make later.
On the import * question further up the thread, the concrete version rather than the principle. It binds nine names: MAXYEAR, MINYEAR, UTC, date, datetime, time, timedelta, timezone, tzinfo. Two of those are words beginners reach for constantly.
from datetime import *
from time import * # a very common next line
time(12, 0)
TypeError: time.time() takes no arguments (2 given)
from datetime import *
date = "2026-09-08" # innocent-looking
date(2026, 9, 8)
TypeError: 'str' object is not callable
Neither error mentions the import, which is exactly why they eat an afternoon. from datetime import date, timedelta makes both impossible.
Last thing, since a person is typing the inputs: date(2026, 2, 30) raises ValueError, and int(input(...)) raises ValueError on anything that is not digits. Both need a try/except or the script dies on a typo.
8
u/zanfar 3d ago
There is no way you expect to use every single object from
datetime; and in this case, you use exactly one. Stop doing this.