Reputation: 3
I'm new to Python and have searched everywhere for days for this solution, so I apologize in advance if the answer is posted somewhere that I've not been able to find. Nothing I find addresses this exactly the way I need.
I'm trying to write code to allow the user to pick a start date and end date. I have a csv file with a field for dates in this format: 1/1/2013 0:00
and I want to allow the user to pick dates from any range within 1/1/2013 to 6/30/2013.
I've imported the following, then my problem starts immediately as I'm getting AttributeError: 'method_descriptor' object has no attribute 'strptime'
, but I'm reading that it is an attribute. I'm also trying to avoid the use of any GUI; just need a straightforward way to allow user input parameters. My endgoal here is to convert it to a tool in ArcGIS for user input parameters, which I can do, but I need to create the script correctly first.
import arcpy, os, sys
from datetime import datetime, date, timedelta
startDate = datetime.date.strptime(userdateString, '%Y-%m-%d')
Thank you so much in advance for any direction you can give! I am quite confused by all of this...
Upvotes: 0
Views: 2796
Reputation: 113924
This eliminates the attribute error:
>>> from datetime import datetime
>>> userdateString = '2013-2-1'
>>> datetime.strptime(userdateString, '%Y-%m-%d')
datetime.datetime(2013, 2, 1, 0, 0)
Note that the code to use depends on precisely what one imports. If I use import date
in place of from datetime import datetime
, note that, as a result of python's naming conventions, the method to use isdatetime.datetime.strptime
rather than datetime.strptime
as above:
>>> import datetime
>>> userdateString = '2013-2-1'
>>> datetime.datetime.strptime(userdateString, '%Y-%m-%d')
datetime.datetime(2013, 2, 1, 0, 0)
ADDENDUM: Yes, you can change date formats:
>>> from datetime import datetime
>>> s = '2-1-2013'
>>> datetime.strptime(s, '%m-%d-%Y')
datetime.datetime(2013, 2, 1, 0, 0)
>>> s = '2/1/2013'
>>> datetime.strptime(s, '%m/%d/%Y')
datetime.datetime(2013, 2, 1, 0, 0)
Upvotes: 1
Reputation: 1184
>>> import datetime
>>> userdateString= '2013-2-1'
>>> startDate = datetime.datetime.strptime(userdateString, '%Y-%m-%d')
>>> print startDate
2013-02-01 00:00:00
You need to include an extra datetime between datetime and strptime.
Upvotes: 0