[GIS] Entering date with arcpy.GetParameterAsText()

arcpydateerror-000539

I want to allow users to enter a date as a parameter through a dialog window in a toolbox script.

I have obviously defined the field with a "Date" data type but something is wrong with the formatting.

arcpy.CalculateField_management(fc, "Date", "01.07.2015", "PYTHON", "")

If I enter "01.07.2015" directly in the FieldCalculator in ArcMap it works, but I can´t define it as a parameter in arcpy? What am I doing wrong?

import sys
import arcpy
from arcpy import env

# allow overwriteOutput
env.overwriteOutput = True

# for Script
FC = arcpy.GetParameterAsText(0)
Date = arcpy.GetParameterAsText(1)

try:

    arcpy.CalculateField_management(fc, "Stand", Date, "PYTHON", "")

except Exception, e:
    # If an error occurred, print line number and error message
    import traceback, sys
    tb = sys.exc_info()[2]
    print "Line %i" % tb.tb_lineno
    print e.message

This is in Modelbuilder. The code runs, but the field is not updated. If I take remove the modelbuilder parameters and enter them in the python script the error is:

import sys
import arcpy
from arcpy import env

# allow overwriteOutput
env.overwriteOutput = True

Date = "01.01.2001"

try:

    arcpy.CalculateField_management(fc, "Stand", Date, "PYTHON", "")


except Exception, e:
    # If an error occurred, print line number and error message
    import traceback, sys
    tb = sys.exc_info()[2]
    print "Line %i" % tb.tb_lineno
    print e.message

ERROR:

Line 18
ERROR 000539: : unexpected EOF while parsing (, line 1)
Fehler beim Ausführen von (CalculateField).

Best Answer

  • First, you are getting FC as parameter/variablebut trying to calculate fc (i.e., Python is case sensitive);
  • Second, if you pick "Date only" option from parameter window for date, your script will just work fine;
  • Third, if you need time information (with or without date), you need to take @spk578 's advice and manipulate Date string;
  • Fourth, I am not sure about that but I think Field Calculator honours your Locale settings, which allows you to enter date as in a format of dd.mm.yyyy, which is not possible for me to do the same in Australia (locale is dd/mm/yyyy)
Related Question