For optimizing a webscraping project I want to compare the user input (date) with the format i want
Question:
I want when a user enters a date, I want it in the following format MM/DD/YYYY, so how can I check if the date the user has entered is the right format.
so this what i have done and it always says that it’s in the wrong format.
date = input("Please enter a date in the following format MM/DD/YYYY: ")
Format = datetime.strptime(date, '%m/%d/%Y')
while date != '%m/%d/%Y':
print("the format you entered is wrong, please make sure you entered the approved format which is (MM/DD/YYYY)")
break
Answers:
If you mean dates formatted as MM/DD/YYYY
vs MM/DD/YYYY
then you can’t as the two value domains overlap for DD between 01 and 12. The best you can do is try to parse the date in the expected format, and if it fails (as you already do with strptime()
), reject invalid dates (or try a list of different format strings as fallback).
When you call datetime.strptime(date, '%m/%d/%Y')
, the method is already checking if the dates are in the correct format, and throws an error if they are not
The line:
Format = datetime.strptime(date, '%m/%d/%Y')
will throw an error if not in the right format. Keep in mind that for dates <= 12, it not throw errors if month and date are flipped.
You should remove these lines:
while date != '%m/%d/%Y':
print("the format you entered is wrong, please make sure you entered the approved format which is (MM/DD/YYYY)")
break
The while
statement is not the same as an if
statement. This will start a loop which is not what you want. The date != '%m/%d/%Y'
always returns true as the date object is not a string, but rather an object.
TLDR: remove the while loop. The strptime method will throw an error if formatting is wrong.
I want when a user enters a date, I want it in the following format MM/DD/YYYY, so how can I check if the date the user has entered is the right format.
so this what i have done and it always says that it’s in the wrong format.
date = input("Please enter a date in the following format MM/DD/YYYY: ")
Format = datetime.strptime(date, '%m/%d/%Y')
while date != '%m/%d/%Y':
print("the format you entered is wrong, please make sure you entered the approved format which is (MM/DD/YYYY)")
break
If you mean dates formatted as MM/DD/YYYY
vs MM/DD/YYYY
then you can’t as the two value domains overlap for DD between 01 and 12. The best you can do is try to parse the date in the expected format, and if it fails (as you already do with strptime()
), reject invalid dates (or try a list of different format strings as fallback).
When you call datetime.strptime(date, '%m/%d/%Y')
, the method is already checking if the dates are in the correct format, and throws an error if they are not
The line:
Format = datetime.strptime(date, '%m/%d/%Y')
will throw an error if not in the right format. Keep in mind that for dates <= 12, it not throw errors if month and date are flipped.
You should remove these lines:
while date != '%m/%d/%Y':
print("the format you entered is wrong, please make sure you entered the approved format which is (MM/DD/YYYY)")
break
The while
statement is not the same as an if
statement. This will start a loop which is not what you want. The date != '%m/%d/%Y'
always returns true as the date object is not a string, but rather an object.
TLDR: remove the while loop. The strptime method will throw an error if formatting is wrong.