<?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: How to calculate date field in python script - formatting question in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/how-to-calculate-date-field-in-python-script/m-p/258253#M19879</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import datetime
import arcpy

arcpy.env.workspace = 'c:/temp/my_gdb.gdb'
fc = 'my_fc'
fld = 'my_date_field'

cur = arcpy.InsertCursor(fc)
row = cur.newRow()
row.setValue(fld, datetime.datetime.now())
cur.insertRow(row)

row = cur.newRow()
row.setValue(fld, datetime.datetime(2010,12,25,12,34)
cur.insertRow(row)

row = cur.newRow()
row.setValue(fld, datetime.datetime.now())
cur.insertRow(row)

a_date = '2010-12-25 13:57:01'
date_fmt = '%Y-%m-%d %H:%M:%S'
row = cur.newRow()
row.setValue(fld, datetime.datetime.strptime(a_date, date_fmt)
cur.insertRow(row)

del row
del cur
&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;See python docs for the date and datetime modules for details. strptime and strftime to go between strings and datetime.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 12:43:07 GMT</pubDate>
    <dc:creator>NiklasNorrthon</dc:creator>
    <dc:date>2021-12-11T12:43:07Z</dc:date>
    <item>
      <title>How to calculate date field in python script - formatting question</title>
      <link>https://community.esri.com/t5/python-questions/how-to-calculate-date-field-in-python-script/m-p/258252#M19878</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am trying to calculate a DATE field on a feature class in a personal geodatabase.&amp;nbsp; I have a date variable that is a string: distDate = "2011-02-11"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am not able to figure out the proper formatting to get it to work in a python script.&amp;nbsp; This is what I have, but it does not update with this date, nor does it error out.&amp;nbsp; In ArcMap, I can use calculate field on the attribute table and successfully update the date with #2011-02-11# (VB).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.CalculateField_management (inFC, "DistDate", "#"+distDate+"#")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I could not find help on the ESRI support site, nor by just googling it.&amp;nbsp; Does anybody have suggestions?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Julie&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 11 Feb 2011 19:10:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-calculate-date-field-in-python-script/m-p/258252#M19878</guid>
      <dc:creator>jduval</dc:creator>
      <dc:date>2011-02-11T19:10:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate date field in python script - formatting question</title>
      <link>https://community.esri.com/t5/python-questions/how-to-calculate-date-field-in-python-script/m-p/258253#M19879</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import datetime
import arcpy

arcpy.env.workspace = 'c:/temp/my_gdb.gdb'
fc = 'my_fc'
fld = 'my_date_field'

cur = arcpy.InsertCursor(fc)
row = cur.newRow()
row.setValue(fld, datetime.datetime.now())
cur.insertRow(row)

row = cur.newRow()
row.setValue(fld, datetime.datetime(2010,12,25,12,34)
cur.insertRow(row)

row = cur.newRow()
row.setValue(fld, datetime.datetime.now())
cur.insertRow(row)

a_date = '2010-12-25 13:57:01'
date_fmt = '%Y-%m-%d %H:%M:%S'
row = cur.newRow()
row.setValue(fld, datetime.datetime.strptime(a_date, date_fmt)
cur.insertRow(row)

del row
del cur
&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;See python docs for the date and datetime modules for details. strptime and strftime to go between strings and datetime.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 12:43:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-calculate-date-field-in-python-script/m-p/258253#M19879</guid>
      <dc:creator>NiklasNorrthon</dc:creator>
      <dc:date>2021-12-11T12:43:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate date field in python script - formatting question</title>
      <link>https://community.esri.com/t5/python-questions/how-to-calculate-date-field-in-python-script/m-p/258254#M19880</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Does anyone know how to use datetime.strptime to calculate a date field using 3 fields that represent year, month, and day? Or perhaps using datetime.strptime to calculate a datefield using a string field of date yyyy/mm/dd?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 21 Jun 2012 15:42:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-calculate-date-field-in-python-script/m-p/258254#M19880</guid>
      <dc:creator>deleted-user-IR249IovB3CN</dc:creator>
      <dc:date>2012-06-21T15:42:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate date field in python script - formatting question</title>
      <link>https://community.esri.com/t5/python-questions/how-to-calculate-date-field-in-python-script/m-p/258255#M19881</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;&amp;nbsp; In ArcMap, I can use calculate field on the attribute table and successfully update the date with #2011-02-11# (VB). &lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Does anyone know how to use datetime.strptime to calculate a date field using 3 fields that represent year, month, and day? Or perhaps using datetime.strptime to calculate a datefield using a string field of date yyyy/mm/dd?&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;An approach that may help both of you is to try the VBScript DateSerial function: &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;DateSerial(2011,02,11)
DateSerial([yearfield],[monthfield],[dayfield])&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Sometimes VBScript is easier, sometimes Python is easier. I'm really happy Esri is letting us have the option!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 12:43:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-calculate-date-field-in-python-script/m-p/258255#M19881</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-11T12:43:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate date field in python script - formatting question</title>
      <link>https://community.esri.com/t5/python-questions/how-to-calculate-date-field-in-python-script/m-p/258256#M19882</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for the VB Dateserial function. I did not find reference to that in all of my searching. Before I saw this response this is the solution a colleague of mine came up with to calculate the new date field from 3 exisitng fields of month, day, and year.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; import system modules&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;import arcpy&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;from datetime import datetime, date, time&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# set workspace environment&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;env = arcpy.env.workspace = "C:/Files/GIS/Projects/Hypoxia/Shrimp_Hypoxia/AnalysisData.gdb"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;#Set OverWrite if files already exist&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.env.overwriteOutput = True&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# set local variables&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;inFile = "Full_Dataset_Albers"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;#Add date field to inFile&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.AddField_management(inFile, "DateField","DATE")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;#Create an UpdateCursor&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;rows = arcpy.UpdateCursor(inFile)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;#Iterate through each record and update the DateField date with the combination of the 3 fields that provide the components of the date.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;for row in rows:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; d_obj = datetime(row.getValue("yr"), row.getValue("mon"), row.getValue("day"))&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.DateField = d_obj&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;del row, rows&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 25 Jun 2012 13:50:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-calculate-date-field-in-python-script/m-p/258256#M19882</guid>
      <dc:creator>deleted-user-IR249IovB3CN</dc:creator>
      <dc:date>2012-06-25T13:50:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate date field in python script - formatting question</title>
      <link>https://community.esri.com/t5/python-questions/how-to-calculate-date-field-in-python-script/m-p/258257#M19883</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Matt, thanks for the update. Before the edit window closes, could you enclose your python code in [thread=48475]code tags[/thread] to make it more useful for others?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Just a note, I like to store datetime in GIS datasets and database tables as standard-format strings "yyyy-mm-dd hh:mm:ss" and calculate date values when needed in an analysis or selection, because it seems every operating system and file format seems to have its own scheme or limitations!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Not an issue if all your data is in one database, but if you need to move the table from one place another, it seems data stored in date format can be more trouble than it is worth!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 25 Jun 2012 15:03:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-calculate-date-field-in-python-script/m-p/258257#M19883</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2012-06-25T15:03:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate date field in python script - formatting question</title>
      <link>https://community.esri.com/t5/python-questions/how-to-calculate-date-field-in-python-script/m-p/258258#M19884</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Matt, thanks for the update. Before the edit window closes, could you enclose your python code in [thread=48475]code tags[/thread] to make it more useful for others?&lt;BR /&gt;&lt;BR /&gt;Just a note, I like to store datetime in GIS datasets and database tables as standard-format strings "yyyy-mm-dd hh:mm:ss" and calculate date values when needed in an analysis or selection, because it seems every operating system and file format seems to have its own scheme or limitations!&lt;BR /&gt;&lt;BR /&gt;Not an issue if all your data is in one database, but if you need to move the table from one place another, it seems data stored in date format can be more trouble than it is worth!&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Sorry but I have a similar problem, I'm writing a script to import multiple .shp to a gdb and I'd like to add date in from of the name (ex: località -&amp;gt;_ 120705_località) &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Any idea?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 05 Jul 2012 14:37:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-calculate-date-field-in-python-script/m-p/258258#M19884</guid>
      <dc:creator>LucaMoiana</dc:creator>
      <dc:date>2012-07-05T14:37:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate date field in python script - formatting question</title>
      <link>https://community.esri.com/t5/python-questions/how-to-calculate-date-field-in-python-script/m-p/258259#M19885</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Sorry but I have a similar problem, I'm writing a script to import multiple .shp to a gdb and I'd like to add date in from of the name (ex: località -&amp;gt;_ 120705_località) &lt;BR /&gt;Any idea?&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import time
# use it in a string as needed e.g.

arcpy.AddMessage("_{0}_località".format(time.strftime("%m%d%y")))

&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This should give you _ "todays date"_località&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 12:43:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-calculate-date-field-in-python-script/m-p/258259#M19885</guid>
      <dc:creator>SolomonPulapkura</dc:creator>
      <dc:date>2021-12-11T12:43:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate date field in python script - formatting question</title>
      <link>https://community.esri.com/t5/python-questions/how-to-calculate-date-field-in-python-script/m-p/258260#M19886</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;This is the solution a colleague of mine came up with to calculate the new date field from 3 exisitng fields of month, day, and year.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
 import system modules
import arcpy
from datetime import datetime, date, time

# set workspace environment
env = arcpy.env.workspace = "C:/Files/GIS/Projects/Hypoxia/Shrimp_Hypoxia/AnalysisData.gdb"

#Set OverWrite if files already exist
arcpy.env.overwriteOutput = True

# set local variables
inFile = "Full_Dataset_Albers"

#Add date field to inFile
arcpy.AddField_management(inFile, "DateField","DATE")

#Create an UpdateCursor
rows = arcpy.UpdateCursor(inFile)
#Iterate through each record and update the DateField date with the combination of the 3 fields that provide the components of the date.
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; d_obj = datetime(row.getValue("yr"), row.getValue("mon"), row.getValue("day"))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.DateField = d_obj
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)
del row, rows
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 12:43:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-calculate-date-field-in-python-script/m-p/258260#M19886</guid>
      <dc:creator>deleted-user-IR249IovB3CN</dc:creator>
      <dc:date>2021-12-11T12:43:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate date field in python script - formatting question</title>
      <link>https://community.esri.com/t5/python-questions/how-to-calculate-date-field-in-python-script/m-p/258261#M19887</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Guys,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Sorry to jump in middle. I have functionality in a script that handles date and check for correct date any tips on how to check 2 different dates in the same layer. This is the sample and I�??m very new to scripting&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;#Field, Year, Month, Day&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;checker.checkGDBFieldForRightDate("SURVEY_DATE", 2012, 07, 24)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank You&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 26 Jul 2012 05:04:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-calculate-date-field-in-python-script/m-p/258261#M19887</guid>
      <dc:creator>DhanushkaJayamaha</dc:creator>
      <dc:date>2012-07-26T05:04:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate date field in python script - formatting question</title>
      <link>https://community.esri.com/t5/python-questions/how-to-calculate-date-field-in-python-script/m-p/258262#M19888</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# Import modules
import arcpy
from datetime import datetime

# Define variables
surveyDateEnteredFld = "DATENTERED" # existing text field
surveyDateFld = "SURVEY_DATE"
expression = "formatDate(!" + surveyDateEnteredFld + "!)"
codeBlock = """def formatDate(datestr):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; d = datetime.datetime.strptime(datestr, '%m%d%Y')
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return d"""

# Add and calculate new date field
arcpy.AddField_management(outputTableView, surveyDateFld, "DATE")
arcpy.CalculateField_management(outputTableView, surveyDateFld, expression, "PYTHON", codeBlock)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 12:43:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-calculate-date-field-in-python-script/m-p/258262#M19888</guid>
      <dc:creator>KristinaGrace</dc:creator>
      <dc:date>2021-12-11T12:43:17Z</dc:date>
    </item>
  </channel>
</rss>

