<?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 Re: Field Calculator with Python parser convert text to date in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/field-calculator-with-python-parser-convert-text/m-p/1214424#M65643</link>
    <description>&lt;P&gt;My apologizes for number of responses.&lt;/P&gt;&lt;P&gt;So field "Year" has just years, 2015,1997, 2022, etc and is a "text" field. It doesn't have month or days&lt;/P&gt;&lt;P&gt;When I replace the "%Y" with "%m/%d/%Y"&lt;/P&gt;&lt;LI-CODE lang="python"&gt;d1 = datetime.strptime(row[0], "%m/%d/%Y").year&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I get;&lt;/P&gt;&lt;P&gt;ERROR 000539: Traceback (most recent call last):&lt;BR /&gt;File "&amp;lt;expression&amp;gt;", line 1, in &amp;lt;module&amp;gt;&lt;BR /&gt;File "&amp;lt;string&amp;gt;", line 11, in calcYear&lt;BR /&gt;TypeError: strptime() argument 1 must be str, not int&lt;BR /&gt;Failed to execute&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If i use the following&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from datetime import datetime
from datetime import time
import datetime
def calcYear():
    fc = "Lyr"
    #testdate = '04/25/2015'
    with arcpy.da.UpdateCursor(fc,['Year','Year1']) as cursor:
        for row in cursor:
            if row[0] not in (""," ",None):
                d1 = datetime.datetime.strptime("{}".format(row[0]),"%Y").year         
                return d1
                #formattedTime = datetime.datetime.strptime("{}".format(!Year!), "%Y")
                #row[1] = d1
                #cursor.updateRow(row)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I get Value = 1997&lt;/P&gt;&lt;P&gt;the Field "Year1" doesn't get updated&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tested this in python window&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
from datetime import datetime
from datetime import time
import datetime
#def calcYear():
fc = "Lyr"
#testdate = '04/25/2015'
with arcpy.da.UpdateCursor(fc,['Year','Year1']) as cursor:
    for row in cursor:
        if row[0] not in (""," ",None):
            d1 = datetime.datetime.strptime("{}".format(row[0]),"%Y").year         
            print (d1)
            #formattedTime = datetime.datetime.strptime("{}".format(!Year!), "%Y")
            row[1] = d1
            cursor.updateRow(row)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp; I get the year printed&lt;/P&gt;&lt;P&gt;2022&lt;BR /&gt;2022&lt;BR /&gt;2022&lt;BR /&gt;2022&lt;BR /&gt;2022&lt;BR /&gt;2022&lt;BR /&gt;2022&lt;BR /&gt;2007&lt;/P&gt;&lt;P&gt;But the field "Year1" doesn't get populated/updated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2022-09-20 16_19_04-.png" style="width: 561px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/51694iA48D349BACF93C9C/image-size/large?v=v2&amp;amp;px=999" role="button" title="2022-09-20 16_19_04-.png" alt="2022-09-20 16_19_04-.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 20 Sep 2022 22:26:52 GMT</pubDate>
    <dc:creator>2Quiker</dc:creator>
    <dc:date>2022-09-20T22:26:52Z</dc:date>
    <item>
      <title>Field Calculator with Python parser convert text to date</title>
      <link>https://community.esri.com/t5/python-questions/field-calculator-with-python-parser-convert-text/m-p/1214321#M65637</link>
      <description>&lt;P&gt;I need to convert a text field "Year1" into a date filed (Year1) with just the year.&lt;/P&gt;&lt;P&gt;Field Year is a text field, with just a year date, "2015" and field Year1 is a date field.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;from datetime import datetime
from datetime import time
import datetime as dt
from time import gmtime, strftime
def calcYear():
    fc = "Lyr"
    #testdate = '04/25/2015'
    with arcpy.da.UpdateCursor(fc,['Year','Year1']) as cursor:
        for row in cursor:
            if row[0] not in (""," ",None):
                d1 = datetime.strptime(datetime.strftime(row[0], "%Y"), "%Y")         
                row[1] = d1.year
                cursor.updateRow(row)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I get the following error. I was thinking that line 11 was converting the text&amp;nbsp; into a day(year).&lt;/P&gt;&lt;P&gt;ERROR 000539: Traceback (most recent call last):&lt;BR /&gt;File "&amp;lt;expression&amp;gt;", line 1, in &amp;lt;module&amp;gt;&lt;BR /&gt;File "&amp;lt;string&amp;gt;", line 11, in calcYear&lt;BR /&gt;TypeError: descriptor 'strftime' requires a 'datetime.date' object but received a 'int'&lt;BR /&gt;Failed to execute (Calculate Field&lt;/P&gt;</description>
      <pubDate>Tue, 20 Sep 2022 19:49:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/field-calculator-with-python-parser-convert-text/m-p/1214321#M65637</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2022-09-20T19:49:36Z</dc:date>
    </item>
    <item>
      <title>Re: Field Calculator with Python parser convert text to date</title>
      <link>https://community.esri.com/t5/python-questions/field-calculator-with-python-parser-convert-text/m-p/1214347#M65640</link>
      <description>&lt;P&gt;&lt;SPAN&gt;The &lt;EM&gt;strftime&lt;/EM&gt;() method returns a string representing date and time &lt;STRONG&gt;&lt;EM&gt;using date, time or datetime object&lt;/EM&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;. Try just strptime.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;d1 = datetime.strptime(row[0], "%Y") &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Edit to add:&lt;/P&gt;&lt;P&gt;This will return 2015-01-01 00:00:00&lt;/P&gt;&lt;P&gt;You can get the year with dot notation, but it is an int.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;d1 = datetime.strptime(xd, "%Y").year&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Sep 2022 19:31:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/field-calculator-with-python-parser-convert-text/m-p/1214347#M65640</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-09-20T19:31:29Z</dc:date>
    </item>
    <item>
      <title>Re: Field Calculator with Python parser convert text to date</title>
      <link>https://community.esri.com/t5/python-questions/field-calculator-with-python-parser-convert-text/m-p/1214356#M65641</link>
      <description>&lt;P&gt;Thanks fro the replay.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Tried the following but nothing happens, no print, no error.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def calcYear():
    fc = "Lyr"
    #testdate = '04/25/2015'
    with arcpy.da.UpdateCursor(fc,['Year','Year1']) as cursor:
        for row in cursor:
            if row[0] not in (""," ",None):
                d1 = datetime.strptime(row[0],"%Y").year     
                print(d1)  #"{}".formate(d1)
                row[1] = d1
                cursor.updateRow(row)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Sep 2022 19:51:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/field-calculator-with-python-parser-convert-text/m-p/1214356#M65641</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2022-09-20T19:51:03Z</dc:date>
    </item>
    <item>
      <title>Re: Field Calculator with Python parser convert text to date</title>
      <link>https://community.esri.com/t5/python-questions/field-calculator-with-python-parser-convert-text/m-p/1214372#M65642</link>
      <description>&lt;P&gt;You need to format it to match the input date format so if your date is mm/dd/YYYY, you'll use %m/%d/%Y:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;d1 = datetime.strptime(row[0], "%m/%d/%Y").year&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 20 Sep 2022 20:22:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/field-calculator-with-python-parser-convert-text/m-p/1214372#M65642</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-09-20T20:22:09Z</dc:date>
    </item>
    <item>
      <title>Re: Field Calculator with Python parser convert text to date</title>
      <link>https://community.esri.com/t5/python-questions/field-calculator-with-python-parser-convert-text/m-p/1214424#M65643</link>
      <description>&lt;P&gt;My apologizes for number of responses.&lt;/P&gt;&lt;P&gt;So field "Year" has just years, 2015,1997, 2022, etc and is a "text" field. It doesn't have month or days&lt;/P&gt;&lt;P&gt;When I replace the "%Y" with "%m/%d/%Y"&lt;/P&gt;&lt;LI-CODE lang="python"&gt;d1 = datetime.strptime(row[0], "%m/%d/%Y").year&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I get;&lt;/P&gt;&lt;P&gt;ERROR 000539: Traceback (most recent call last):&lt;BR /&gt;File "&amp;lt;expression&amp;gt;", line 1, in &amp;lt;module&amp;gt;&lt;BR /&gt;File "&amp;lt;string&amp;gt;", line 11, in calcYear&lt;BR /&gt;TypeError: strptime() argument 1 must be str, not int&lt;BR /&gt;Failed to execute&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If i use the following&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from datetime import datetime
from datetime import time
import datetime
def calcYear():
    fc = "Lyr"
    #testdate = '04/25/2015'
    with arcpy.da.UpdateCursor(fc,['Year','Year1']) as cursor:
        for row in cursor:
            if row[0] not in (""," ",None):
                d1 = datetime.datetime.strptime("{}".format(row[0]),"%Y").year         
                return d1
                #formattedTime = datetime.datetime.strptime("{}".format(!Year!), "%Y")
                #row[1] = d1
                #cursor.updateRow(row)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I get Value = 1997&lt;/P&gt;&lt;P&gt;the Field "Year1" doesn't get updated&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tested this in python window&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
from datetime import datetime
from datetime import time
import datetime
#def calcYear():
fc = "Lyr"
#testdate = '04/25/2015'
with arcpy.da.UpdateCursor(fc,['Year','Year1']) as cursor:
    for row in cursor:
        if row[0] not in (""," ",None):
            d1 = datetime.datetime.strptime("{}".format(row[0]),"%Y").year         
            print (d1)
            #formattedTime = datetime.datetime.strptime("{}".format(!Year!), "%Y")
            row[1] = d1
            cursor.updateRow(row)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp; I get the year printed&lt;/P&gt;&lt;P&gt;2022&lt;BR /&gt;2022&lt;BR /&gt;2022&lt;BR /&gt;2022&lt;BR /&gt;2022&lt;BR /&gt;2022&lt;BR /&gt;2022&lt;BR /&gt;2007&lt;/P&gt;&lt;P&gt;But the field "Year1" doesn't get populated/updated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2022-09-20 16_19_04-.png" style="width: 561px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/51694iA48D349BACF93C9C/image-size/large?v=v2&amp;amp;px=999" role="button" title="2022-09-20 16_19_04-.png" alt="2022-09-20 16_19_04-.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Sep 2022 22:26:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/field-calculator-with-python-parser-convert-text/m-p/1214424#M65643</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2022-09-20T22:26:52Z</dc:date>
    </item>
    <item>
      <title>Re: Field Calculator with Python parser convert text to date</title>
      <link>https://community.esri.com/t5/python-questions/field-calculator-with-python-parser-convert-text/m-p/1214524#M65647</link>
      <description>&lt;P&gt;Spaced that you are doing this in a field calculator. You don't use a cursor in a field calculator because it will iterate over all features for each feature. Field Calculator has its own iterator so you do not have to set it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Create the calculator on the target date field. Pass in the string field and return the value.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;calcYear(!Year!)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import datetime
def calcYear(strYear):
    return datetime.datetime.strptime(strYear, '%Y')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Sep 2022 10:30:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/field-calculator-with-python-parser-convert-text/m-p/1214524#M65647</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-09-21T10:30:55Z</dc:date>
    </item>
    <item>
      <title>Re: Field Calculator with Python parser convert text to date</title>
      <link>https://community.esri.com/t5/python-questions/field-calculator-with-python-parser-convert-text/m-p/1216725#M65698</link>
      <description>&lt;P&gt;Sorry for the delay, I got caught up.&lt;/P&gt;&lt;P&gt;I get the following with what you suggested.&lt;/P&gt;&lt;P&gt;ERROR 000539: Traceback (most recent call last):&lt;BR /&gt;File "&amp;lt;expression&amp;gt;", line 1, in &amp;lt;module&amp;gt;&lt;BR /&gt;File "&amp;lt;string&amp;gt;", line 3, in calcYear&lt;BR /&gt;TypeError: strptime() argument 1 must be str, not int&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I get the following If I do this .&lt;/P&gt;&lt;P&gt;WARNING 002858: Certain rows set to NULL due to error while evaluating python expression: File "&amp;lt;string&amp;gt;", line 3, in calcYear&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;calcYear(!Year!)&lt;/LI-CODE&gt;&lt;LI-CODE lang="python"&gt;import datetime
def calcYear(strYear):
    return datetime.datetime.strptime("{}".format(strYear), '%Y') &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There is nulls in this field, how do I get past those nulls?&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import datetime
def calcYear(strYear):
    if (strYear == None):
        return 0
    else:
        return datetime.datetime.strptime("{}".format(strYear), '%Y') &lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 27 Sep 2022 20:32:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/field-calculator-with-python-parser-convert-text/m-p/1216725#M65698</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2022-09-27T20:32:20Z</dc:date>
    </item>
    <item>
      <title>Re: Field Calculator with Python parser convert text to date</title>
      <link>https://community.esri.com/t5/python-questions/field-calculator-with-python-parser-convert-text/m-p/1217571#M65715</link>
      <description>&lt;P&gt;If strYear is None, why not just return None so the date has NULL since the year has NULL?&lt;/P&gt;</description>
      <pubDate>Thu, 29 Sep 2022 19:24:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/field-calculator-with-python-parser-convert-text/m-p/1217571#M65715</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2022-09-29T19:24:37Z</dc:date>
    </item>
  </channel>
</rss>

