#Calculate Date Field expression = time.strftime("%Y") arcpy.CalculateField_management("Boundary", "Date", expression, "PYTHON")
#Calculate Date Field import time import datetime now = datetime.date.today() expression = now.strftime("%m%d%y") arcpy.CalculateField_management("Boundary", "Date", expression, "PYTHON")
Solved! Go to Solution.
Does this python code also work in the field calculator inside of the attribute table? I have an active edit session on my feature class in the geodatabase and I am trying to get the current date for editing.
Is the field you're calculating a date type field or text? Either way, you can use these types of code snippets in Field Calculator, but you may want/need the Advanced codeblock to set up a def returning your value.
Hello Greg,
After I posted this if found out that there is a default "data" field calculation in the field calculator tool. It was handy and much easier than trying to write my own.
Thank you.
As Greg said, it can be done... a snippet designed for a text field
>>> print datetime.strftime(datetime.today(),"%Y-%m-%d")
2014-12-10
>>>
as a function it would have to be
Parser: Python
from datetime import datetime
def (dateFld):
datetime.strftime(datetime.today(),"%Y-%m-%d")
Expression box: !YourDateFieldName!
Hey Dan,
After I posted this if found out that there is a default "data" field calculation in the field calculator tool. It was handy and much easier than trying to write my own.
Thank you.
>>> from time import strftime >>> strftime("%m-%d-%Y %H:%M:%S") '06-13-2013 11:52:00' >>> strftime("%m-%d-%Y") '06-13-2013' >>> strftime("%m%d%y") '061313' >>> strftime("%m%d%Y") '06132013' >>> strftime("%Y") '2013' >>>
#Calculate Date Field import time import datetime #current date d1 = datetime.today() _currdate = datetime.strftime(d1, "%Y-%m-%d") arcpy.CalculateField_management("Boundary", "Date", _currdate, "PYTHON")