Select to view content in your preferred language

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

2511
4
08-15-2012 03:43 PM
ChrisHanes
Emerging Contributor
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?
Tags (2)
0 Kudos
4 Replies
SolomonPulapkura
Frequent Contributor
format to pass string with quotes is
"\"yyyymmdd\""


hope this helps.
0 Kudos
ChrisHanes
Emerging Contributor
This tells me "unexpected character after line continuation character":

gp.CalculateField_management(stat_3e, "tognum", "format( [PatIssDate], "\"yyyymmdd\""")", "VB", "")


stat_3e is my input layer, "tognum" is field name to calculate, PatIssDate is field I am calculating from.

Can you post the entire line of code? I'm not sure I inserted your code right. It seems to have issue with the first slash in your portion (I made red) because PyScripter changed the color of it.
0 Kudos
ChrisHanes
Emerging Contributor
I actually misspoke about the source field.  It is actually text, not a date field.  But I still have the same issue with using the format command in Python.

A developer I spoke to suggested not using the command at all saying it probably wouldn't work in 10.1 given the lack of support for VB.

He suggested using a code block to calculate it manually (parsing by the slash).
0 Kudos
RobertMartin2
Deactivated User
It looks like VB is supported - in fact it's the default - in 10.1.

http://resources.arcgis.com/en/help/main/10.1/index.html#/Calculate_Field/00170000004m000000/

Good luck with this!
0 Kudos