0

In my ArcPy script I have an input parameter for the Date, so the user is asked to input a date value (time is not needed) using the data type GPDate. In the script I calculate a new field, field data type DATE, which should include for all features the date that the user entered.

I cannot find a way how to populate this new field with the value of the input parameter. My code:

date = parameters[0].valueAsText
arcpy.management.CalculateField(myFC, "myField", date, "PYTHON3", "", "DATE")

I already tried 100 varieties with datetime, strftime and different formatting of the date (my default input is dd.mm.YYYY), but either I get an error indicating "unexpected EOF while parsing" or that the value cannot be written to the attribute table.

2
  • Have you tried parameters[0].value instead of valueAsText? – user2856 Mar 10 at 6:55
  • Yes, I also tried paraemters[0].value, but no difference – GIS_USAr Mar 10 at 6:56
0

I found out how to do it. It doesn't matter, if the Date field is defined as GPString or GPDate for the parameter and it also doesn't matter if the calculated field is defined as TEXT or DATE.

Important is, that the date which the user inputs, is converted to a string and formatted, so that it has an an apostrophe before and after the date.

Solution:

date = parameters[0].valueAsText
date = """'""" + str(date) + """'"""
arcpy.management.CalculateField(myFC, "myField", date, "PYTHON3", "", "DATE")

The three """ are needed so that the ' is escaped and the code between is not registered as a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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