<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Date field to number field (yyyy/mm/dd) in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/date-field-to-number-field-yyyy-mm-dd/m-p/682222#M52830</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am trying to use calculate field to populate a number field with a date.&amp;nbsp; In ArcMap/Catalog, the field calculation would be like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;field1 = "format([field2], "yyyymmdd")&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Where field1 is a number field and field2 is a date field&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I cannot get this to work in Python though.&amp;nbsp; I tried several different ways and it throws errors when it gets there:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;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", "")&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;None of these worked.&amp;nbsp; I made a model (ran it to test) and outputted to a script.&amp;nbsp; It gave me the same syntax as the last one (with the back slashes).&amp;nbsp; I ran that in my script and it still failed.&amp;nbsp; I then tried making a feature class from the layer first, thinking it might not want a layer input and that didn't help.&amp;nbsp; 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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Can anybody help me pass the string value with imbedded quotes in there correctly?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 15 Aug 2012 22:43:23 GMT</pubDate>
    <dc:creator>ChrisHanes</dc:creator>
    <dc:date>2012-08-15T22:43:23Z</dc:date>
    <item>
      <title>Date field to number field (yyyy/mm/dd)</title>
      <link>https://community.esri.com/t5/python-questions/date-field-to-number-field-yyyy-mm-dd/m-p/682222#M52830</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am trying to use calculate field to populate a number field with a date.&amp;nbsp; In ArcMap/Catalog, the field calculation would be like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;field1 = "format([field2], "yyyymmdd")&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Where field1 is a number field and field2 is a date field&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I cannot get this to work in Python though.&amp;nbsp; I tried several different ways and it throws errors when it gets there:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;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", "")&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;None of these worked.&amp;nbsp; I made a model (ran it to test) and outputted to a script.&amp;nbsp; It gave me the same syntax as the last one (with the back slashes).&amp;nbsp; I ran that in my script and it still failed.&amp;nbsp; I then tried making a feature class from the layer first, thinking it might not want a layer input and that didn't help.&amp;nbsp; 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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Can anybody help me pass the string value with imbedded quotes in there correctly?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Aug 2012 22:43:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/date-field-to-number-field-yyyy-mm-dd/m-p/682222#M52830</guid>
      <dc:creator>ChrisHanes</dc:creator>
      <dc:date>2012-08-15T22:43:23Z</dc:date>
    </item>
    <item>
      <title>Re: Date field to number field (yyyy/mm/dd)</title>
      <link>https://community.esri.com/t5/python-questions/date-field-to-number-field-yyyy-mm-dd/m-p/682223#M52831</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;format to pass string with quotes is &lt;/SPAN&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;"\"yyyymmdd\""&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;hope this helps.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 16 Aug 2012 15:32:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/date-field-to-number-field-yyyy-mm-dd/m-p/682223#M52831</guid>
      <dc:creator>SolomonPulapkura</dc:creator>
      <dc:date>2012-08-16T15:32:56Z</dc:date>
    </item>
    <item>
      <title>Re: Date field to number field (yyyy/mm/dd)</title>
      <link>https://community.esri.com/t5/python-questions/date-field-to-number-field-yyyy-mm-dd/m-p/682224#M52832</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;This tells me "unexpected character after line continuation character":&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE&gt;gp.CalculateField_management(stat_3e, "tognum", "format( [PatIssDate], "\"yyyymmdd\""")", "VB", "")&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;stat_3e is my input layer, "tognum" is field name to calculate, PatIssDate is field I am calculating from.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 16 Aug 2012 15:42:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/date-field-to-number-field-yyyy-mm-dd/m-p/682224#M52832</guid>
      <dc:creator>ChrisHanes</dc:creator>
      <dc:date>2012-08-16T15:42:38Z</dc:date>
    </item>
    <item>
      <title>Re: Date field to number field (yyyy/mm/dd)</title>
      <link>https://community.esri.com/t5/python-questions/date-field-to-number-field-yyyy-mm-dd/m-p/682225#M52833</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I actually misspoke about the source field.&amp;nbsp; It is actually text, not a date field.&amp;nbsp; But I still have the same issue with using the format command in Python.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;He suggested using a code block to calculate it manually (parsing by the slash).&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 16 Aug 2012 16:57:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/date-field-to-number-field-yyyy-mm-dd/m-p/682225#M52833</guid>
      <dc:creator>ChrisHanes</dc:creator>
      <dc:date>2012-08-16T16:57:19Z</dc:date>
    </item>
    <item>
      <title>Re: Date field to number field (yyyy/mm/dd)</title>
      <link>https://community.esri.com/t5/python-questions/date-field-to-number-field-yyyy-mm-dd/m-p/682226#M52834</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;It looks like VB is supported - in fact it's the default - in 10.1.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#/Calculate_Field/00170000004m000000/"&gt;http://resources.arcgis.com/en/help/main/10.1/index.html#/Calculate_Field/00170000004m000000/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Good luck with this!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 16 Aug 2012 17:26:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/date-field-to-number-field-yyyy-mm-dd/m-p/682226#M52834</guid>
      <dc:creator>RobertMartin2</dc:creator>
      <dc:date>2012-08-16T17:26:09Z</dc:date>
    </item>
  </channel>
</rss>

