arcpy script tool - format parameter Date data type

2398
4
01-24-2017 08:57 AM
NatashaJurko
New Contributor III

I have written a script and have an arcpy tool in a custom toolbox to fill in the parameters. 

Two of my parameters are dates. 

The Date data-type format for the parameters has a default setting of date and time format to the script.

I'd like to only pass the date onto my script.  I included some screenshots of my parameters and the date data-type in the tool.

This old geonet question asks the same sort of thing for a python toolbox (.pyt) but doesn't provide an answer.

Has anyone found a way to access the properties of the date data type?  I haven't been able to find any help in the documentation.

I'm working in ArcGIS 10.4.

This is my last-ditch effort before I strip it apart myself in the validation.

Thanks,

Natasha

Reply
0 Kudos
4 Replies
DanPatterson_Retired
MVP Esteemed Contributor

can't find anything so I presume that you are going to have to do the split thing

>>> from datetime import datetime
>>> str(datetime.now())
'2017-01-26 17:21:42.613751'
>>> str(datetime.now()).split(" ")[0]
'2017-01-26'
NatashaJurko
New Contributor III

Yes, I ended up splitting the date/time components like you mentioned Dan.  

I found some interesting things with the Date data type:

  1.  Even if you select date only radio button the parameter still 'holds' the time.  It will be set to 00:00:00, but does not get passed onto your arcpy script though.
  2. If you select Time only, the date is still stored in the parameter.  It is set to 1899-12-30.  But it too will not be passed onto your arpcy script.
  3. If you program in the validation code to strip out the time, the radio button changes automatically.

I found those funny things with the date/time because I wanted to send an error if a Time type was entered.  The validation always returned true when looking for time, even when there was only a date in my parameter.

Below is how I went about striping out the time and keeping only the date.  I am using this tool with different date formats which is why you see a reference to a dictionary.   And the second chunk of code shows how I use this function in the Update Messages potion of the validation.

def checkIfTimeOnly(self, winDate, theDate):
    dateEdit = str(theDate)
    dateSplit = dateEdit.split()
    dateDate = dateSplit[0]
    zeroDate = datetime.strptime("1899-12-30", "%Y-%m-%d").date()
    dateForm = datetime.strptime(str(dateDate), date_dictionary[winDate]).date()
    #if date is the zero date then we have a time type, not date or date/time type
    if dateForm == zeroDate:
        return True
    else:
        return False
if self.params[7].altered:
        if self.params[7].value:
            isTime = self.checkIfTimeOnly(winDateValue, self.params[7].value)
            if isTime:
                self.params[7].setErrorMessage("Is not a date")
            if self.params[8].value == None:
                self.params[8].value = self.params[7].value

    if self.params[8].altered:
        if self.params[8].value:
            isTime = self.checkIfTimeOnly(winDateValue, self.params[8].value)
            if isTime:
                self.params[8].setErrorMessage("Is not a date")
            #Check param8 or End Date is not before the start/select date.
            if self.params[7].value and self.params[8].value:
                if self.params[7].value > self.params[8].value:
                    self.params[8].setErrorMessage("End date must be later than Start Date.")

Cheers,

Natasha

DanPatterson_Retired
MVP Esteemed Contributor

good to note... I hate dates anyway, so I just usually convert to string and work from that, but I guess when you don't have the luxury to fix existing tables, this will come in handy

Reply
0 Kudos
GeorgHaefele
New Contributor III

Unfortunately the Esri documentation on Python toolboxes is indeed very poor. 

With trial and error, I found out that the field must be initialized with a date value. If the value is set  as date in the getParameterInfo function, then Date is selected in the date-time picker.

For example (param3 is datatype GPDate):

param3.value = "1/1/2020"

Date-time picker in ArcGIS Pro

Reply
0 Kudos