In ArcToolbox I have a script (arcpy) that calculates some info plus a date into a database table. The date required to write is often (but not always) the current date, so the tool asks for input of the date to write.

enter image description here

I'd like to have the Tool automatically populate the current date into this input field so that the user only needs to modify the date on the occasions where the date is not today.

How can I set the Default Value to be the current date? I only seem to be able to enter dates into the Default field in the tool properties (as it's a Date field), so don't seem to be able to use anything like date.today() or similar.

enter image description here


You have to modify validation script, e.g.:

import arcpy, datetime
class ToolValidator(object):
  """Class for validating a tool's parameter values and controlling
  the behavior of the tool's dialog."""

  def __init__(self):
    """Setup arcpy and the list of tool parameters."""
    self.params = arcpy.GetParameterInfo()

  def initializeParameters(self):
    """Refine the properties of a tool's parameters.  This method is
    called when the tool is opened."""

To do this I think you should look into Tool Validation.

This won't be the whole answer to your question, but the code I use to get today's date as a string in a Python Toolbox tool is:

import datetime
now = datetime.datetime.now()
now.replace(minute=0, hour=0, second=0, microsecond=0)
today = now.date()
yyyy = str(today).split("-")[0]
mm = str(today).split("-")[1]
dd = str(today).split("-")[2]
todayStr = "{0}/{1}/{2}".format(dd,mm,yyyy)

Perhaps, try this along with the advice offered by @FelixIP.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.