I’m trying to make it so that it checks if its in the format I want and if its greater than today’s date. I want it to ask for input until it gets a right answer but I can’t find a way to do so.
JavaScript
x
8
1
while True:
2
try:
3
x=dt.datetime.strptime(input(), '%d/%m/%Y')
4
5
break
6
except ValueError:
7
print(1)
8
Advertisement
Answer
You can use the today
class method to get a datetime
object representing today’s date, and use the >
or <
operator to compare it to the parsed x
:
JavaScript
1
10
10
1
while True:
2
try:
3
x = dt.datetime.strptime(input(), '%d/%m/%Y')
4
if x > dt.datetime.today():
5
break
6
else:
7
print('before today')
8
except ValueError:
9
print('wrong format')
10