Skip to content Skip to sidebar Skip to footer

How Do I Take Input In The Date Time Format?

I have written a code which take start_date and end_date from user but it is throwing an error. Following is the code: from datetime import datetime start_date = datetime.strptime(

Solution 1:

Since you are using print statement, seems like you are using Python 2.x .

In Python 2.x , input() actually evaluates the result and returns, so, if you are entering something like 1/1/2014 or some number, you would get an integer (which in this case is 1 divided by 1 divided by 2014 -> 0 ), not a string. And it is generally dangerous to use input() , since user can input anything and that would get evaluated.

Use raw_input() instead, to get the string, example -

start_date = datetime.strptime(raw_input('Enter Start date in the format m/d/y'), '%m/%d/%Y')
end_date = datetime.strptime(raw_input('Enter End date in the format m/d/y'), '%m/%d/%Y')

In Python 3.x , raw_input() from Python 2 was renamed to input() , so using input() in python 3 is same as using raw_input() in Python 2.x .

Solution 2:

For Python 2 use raw_input instead of input. For Python 3 input is fine.

Post a Comment for "How Do I Take Input In The Date Time Format?"