1

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).
1
  • The problem you're coming across is the Date variable is assigned as a string.I think you are looking to convert that string into a date data type by using arcpy.ConvertTimeField_management(). Have a look at this discussion for more converting dates and strings. resources.arcgis.com/en/help/main/10.2/index.html#//…
    – spk578
    Commented Jun 16, 2015 at 11:09

2 Answers 2

3
  • 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)
0

My Solution was:

ErfDat = "'" + "01.06.2015" + "'"

The date needs to be wrapped

1

Not the answer you're looking for? Browse other questions tagged or ask your own question.