Python arcpy.GetParameterAsText() for a Date field

702
1
05-07-2018 01:16 PM
JoseSanchez
Occasional Contributor III

Hello everyone,

This is my first Geoprocessing Service based on a Python script tool.. How do I process a datetime field in a Python script tool?

I wrote a Python script tool and one of the parameter fields gets a DateTime value. This field fails with the following error message.

Python tool Parameters (Properties --> Parameters)

  • RequestedDate   Date
  • Rest of parameters are numeric or text fields

Python source Code:

in_requesteddate = arcpy.GetParameterAsText(5)

arcpy.AddMessage("in_requesteddate = " + in_requesteddate)

in_featureclass = arcpy.GetParameter(6)

requested_datetime = datetime.strptime(in_requesteddate, '%m/%d/%Y')

Error Message:

Traceback (most recent call last):
  File "C:\Workspace\GeocoprocessingServices\Sample\Toolbox.tbx#Script.py", line 25, in <module>
AttributeError: 'module' object has no attribute 'strptime'

Is this field processed as a text field or as a DateTime field? 

Do I need this conversion datetime.strptime .....

Thanks

0 Kudos
1 Reply
DanPatterson_Retired
MVP Esteemed Contributor

check how datetime was imported

was it like

from datetime import datetime

or was it like

import datetime

since the strptime func is in datetime.datetime

dir(datetime.datetime)

[... snip....
 'strftime', 'strptime', 'time',... more snip...]
0 Kudos