hanesc

Date field to number field (yyyy/mm/dd)

Discussion created by hanesc on Aug 15, 2012
Latest reply on Aug 16, 2012 by rmartin4
I am trying to use calculate field to populate a number field with a date.  In ArcMap/Catalog, the field calculation would be like this:

field1 = "format([field2], "yyyymmdd")


Where field1 is a number field and field2 is a date field

I cannot get this to work in Python though.  I tried several different ways and it throws errors when it gets there:

gp.CalculateField_management(input_layer, "field1", "format([field2], "yyyymmdd")", "VB", "")

Expression1 = """format([field2],"yyyymmdd")"""
gp.CalculateField_management(input_layer, "field1", Expression1, "VB", "")

gp.CalculateField_management(input_layer, "field1", "format([field2], \"yyyymmdd\")", "VB", "")


None of these worked.  I made a model (ran it to test) and outputted to a script.  It gave me the same syntax as the last one (with the back slashes).  I ran that in my script and it still failed.  I then tried making a feature class from the layer first, thinking it might not want a layer input and that didn't help.  I tried one last change (making it "PYTHON_9.3" instead of "VB" in the line of code and that crashed it when it ran.

Can anybody help me pass the string value with imbedded quotes in there correctly?

Outcomes